-2

Possible Duplicate:
Reference - What does this symbol mean in PHP?

I am trying to learn PHP and programming. In the book I am studying there is something like: $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;

$flags is used for the split() method.

The first flag is: If this flag is set, only non-empty pieces will be returned by preg_split(). The second is: If this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well.

Why isn't he using && operator but the | ?

Can you please explain what | does actually?

As far as I know these are

Community
  • 1
  • 1
Koray Tugay
  • 20,438
  • 37
  • 155
  • 276
  • 1
    `|` and related operators are bitwise operators. Using bitfields you can pass an integer with certain bits turned on or off to enable or disable certain options. – datasage Jan 20 '13 at 20:11
  • The boolean `1 && 2` gives you true (`1`). While `1 | 2` is `3`, as would be `1 + 2` (which is equivalent to the bitwise OR if all operands have distinct bits set in binary notation). – mario Jan 20 '13 at 20:13
  • So, you know that it is a bitwise operator but did not try to find out what that *means*? – Fabian Schmengler Jan 20 '13 at 20:31
  • @datasage So it is like passing a whole different integer? Like if PREG_SPLIT_NO_EMPTY is 1 and PREG_SPLIT_DELIM_CAPTURE is 2 then when I do | for them, I am passing a different integer to the function? – Koray Tugay Jan 20 '13 at 20:38

2 Answers2

4

&& is the logical AND whereas | is a bitwise operator.

a && b evalutes to true when both operands are evaluted to true. Since flags are almost always numbers greater than 1, this expression always evalutes to true.

How functions accepting bitmasks work

They specify constants

Note that these constants have to have powers of 2:

FLAG_INT1 = 1
FLAG_INT2 = 2
FLAG_INT3 = 4
FLAG_INT4 = 8

You call the function combining some flags

myFunction(FLAG_INT1 | FLAG_INT3)

This leads to a bitwise OR operation:

   0001
OR 0100
=======
   0101

A set bit (1) in one (or both) of the operands will lead to a set bit (1) in the result, too.

The function checks internally for each flag

This requires a bitwise AND operation:

    0101
AND 0001 // check for FLAG_INT1
========
    0001 // true

    0101
AND 0010 // check for FLAG_INT2
========
    0000 // false

    0101
AND 0100 // check for FLAG_INT3
========
    0100 // true

The bitwise AND required both operands to have a set bit (1) at position X in order to result in a set bit (1) in the result at position X.

Wikipedia has also a nice article about the common bitwise operators: http://en.wikipedia.org/wiki/Bitwise_operation

ComFreek
  • 27,416
  • 16
  • 97
  • 150
  • Thanks, this is a great explanation, but what I do not understand is that the function gets an Integer. So when I do int1 | int2 for a int parameters, then I am neither passing int1 nor int2 but a whole different integer to the function? – Koray Tugay Jan 20 '13 at 20:39
  • @KorayTugay The *whole different integer* is not as differnt as you think, it's a combination of *int1* and *int2*. Now you might ask where the function does know about that it has been composed of *int1* and *int2*. This is as simple as a bitwise AND operation: `if (flags & int1)` where *flags* is the given parameter. – ComFreek Jan 20 '13 at 20:43
  • You mean if ( int1 & int2 ) ? – Koray Tugay Jan 20 '13 at 20:44
  • @KorayTugay No, *flags & int 2* because you have to check whether *int2* is contained int *flags*. See the update, too. – ComFreek Jan 20 '13 at 20:48
0

&& is the Logical "AND" but the | is a bitwise

Zz Oussama
  • 229
  • 1
  • 12