2

I have been rooting around with PHP for a while now but I've stumbled across something that I can't make heads or tails of. It is from a post that I found on here (that I believe has been locked) and I understand most of the solution, but there's just one part that I'm confused on.

  if ( ( $number & $error_number ) == $number )
  {
    $error_description[ ] = $description;
  }

I'm not quite sure what is being checked here. Any help is appreciated.

((Full code))

<?php

$error_number = 22527; //could also use ini_get('error_reporting')
$error_description = array( );
$error_codes = array(
    E_ERROR              => "E_ERROR",
    E_WARNING            => "E_WARNING",
    E_PARSE              => "E_PARSE",
    E_NOTICE             => "E_NOTICE",
    E_CORE_ERROR         => "E_CORE_ERROR",
    E_CORE_WARNING       => "E_CORE_WARNING",
    E_COMPILE_ERROR      => "E_COMPILE_ERROR",
    E_COMPILE_WARNING    => "E_COMPILE_WARNING",
    E_USER_ERROR         => "E_USER_ERROR",
    E_USER_WARNING       => "E_USER_WARNING",
    E_USER_NOTICE        => "E_USER_NOTICE",
    E_STRICT             => "E_STRICT",
    E_RECOVERABLE_ERROR  => "E_RECOVERABLE_ERROR",
    E_DEPRECATED         => "E_DEPRECATED",
    E_USER_DEPRECATED    => "E_USER_DEPRECATED",
    E_ALL                => "E_ALL"
);
foreach( $error_codes as $number => $description )
{        
    if ( ( $number & $error_number ) == $number )
    {
        $error_description[ ] = $description;
    }
}
echo sprintf(
    "error number %d corresponds to:<br>\n%s",
    $error_number,
    implode( " | ", $error_description )
);
?>

I understand that the key's in the array are predefined PHP constants, but I'm not sure how that last if statement is working/what it's evaluating.

3

2 Answers 2

5

The & operator is a Bitwise Operator which, when used, will return a value with the "bits" set in both variables, in this case $number and $error_number.

If the current $error_number contains the bits of $number, then it contains that error (if that makes sense?).

For example (in binary):

0001 & 1000 = 0000
0001 & 0111 = 0001
0110 & 1111 = 0110

The results show values where there is a bit set (i.e. 1) in both values being ANDed together.

Another example (with error numbers):

$error_number = E_USER_DEPRECATED | E_WARNING | E_ERROR;

if ($error_number & E_WARNING) echo 'E_WARNING'; // will output
if ($error_number & E_PARSE)   echo 'E_PARSE';   // will not output
Sign up to request clarification or add additional context in comments.

Comments

1

& is the bitwise AND operator. Read up on how the bitwise logical operations work here.

In this case, the first number is a so-called bitfield, meaning all bits represent a different error. By performing an AND with the mask of relevant errors - if 0 results none of the requested bits was set, else one of them was.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.