17

Example:

set_error_handler(array($this, 'handleError'), E_ALL & ~E_STRICT & ~E_WARNING & ~E_NOTICE);

what does that suppose to mean?

openfrog
  • 40,201
  • 65
  • 225
  • 373

5 Answers5

27

It is the bitwise not operator (also called "complement"). That is the bits set in ~ $a are those that are not set in $a.

So then

E_ALL & ~E_STRICT & ~E_WARNING & ~E_NOTICE

is the bits set in E_ALL and those not set in E_STRICT, E_WARNING and E_NOTICE. This basically says all errors except strict, warning and notice errors.

Cliff Burton
  • 3,414
  • 20
  • 33
  • 47
jason
  • 236,483
  • 35
  • 423
  • 525
17

It's the bitwise-not operator. For example the bitwise negation of a number with binary representation 01011110 would be 10100001; every single bit is flipped to its opposite.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
sth
  • 222,467
  • 53
  • 283
  • 367
  • 1
    Although that is the technical explanation, that explanation isn't helpful unless you realize that `E_STRICT`, `E_WARNING`, `E_NOTICE` et. al. are integers and are simultaneously being combined with the `&` bitwise operator. – IQAndreas Aug 21 '14 at 13:46
7

The distinction between bitwise (&, |, ~) and non-bitwise (&&, ||, !) operators is that bitwise are applied across all bits in the integer, while non-bitwise treat an integer as a single "true" (non-zero) or "false" (zero) value.

Say, $flag_1 = 00000001 and $flag_2 = 00000010. Both would be "true" for non-bitwise operations, ($flag_1 && $flag_2 is "true"), while the result of $flag_1 & $flag_2 would be 00000000 and the result of $flag_1 | $flag_2 would be 00000011. ~$flag_2 would be 11111101, which when bitwise-ANDed to a running result would clear that bit position (xxxxxx0x). $flag_2 bitwise-ORed to a running result would set that bit position (xxxxxx1x).

Phil Perry
  • 2,126
  • 14
  • 18
1

See Bitwise Operators : it's the "not" operator (quoting) :

~ $a
Bits that are set in $a are not set, and vice versa.


Which means, taking an example inspired from what you posted, that this portion of code :

var_dump(decbin(E_STRICT));
var_dump(decbin(~E_STRICT));

Will get you this output :

string '100000000000' (length=12)
string '11111111111111111111011111111111' (length=32)

(Add a couple of 0 for padding on the left of the first line, and you'll see what I mean)


Removing the padding from the second output, you get :

100000000000
011111111111

Which means the ~ operator gave a 0 bit for each bit that was equal to 1 in the intput -- and vice-versa,

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
1

It's the not bitwise operator. Read about bitwise operators here:

http://php.net/manual/en/language.operators.bitwise.php

philfreo
  • 41,941
  • 26
  • 128
  • 141