Search code examples
phperror-reporting

Why do you have to do a binary operation in error_reporting?


It may sounds like a stupid question, but there is something I don't understand about error_reporting and I could not find the explanation on php.net or after a research on google.

My local workstation error is this :

error_reporting(E_ALL - E_DEPRECATED - E_WARNING);

Everyone else at work is :

error_reporting(E_ALL & ~E_DEPRECATED & ~E_WARNING);

My question is : why does it give the same result ? And why do you have to use a binary operation and not a simple substraction?

Thank you in advance.


Solution

  • Because E_DEPRECATED and E_WARNING have completly different bits set and you substract them from E_ALL were all bits are set.

    The real difference occurs, if you try to remove two times the same bitset or combined bitsets. In this case, you would remove the 'bits' more than once, which leads to completely wrong values.

    In general, you would probably just remove a single option from the error flags and don't know, which bits are currently set

    $level = error_reporting();
    // at this point, you simply can't use simple math, as you don't know, if the given error reporting option is set or not
    $level = $level & ~E_WARNING;
    error_reporting($level);