Question

code1 is a code from zencart core php file, I am confused what it is.
is code1 equal to code2 ?
and what is the & ~ meanning ?

<?php 
/* code1 */
$errors_to_log = (version_compare(PHP_VERSION, 5.3, '>=') ? E_ALL & ~E_DEPRECATED & ~E_NOTICE : version_compare(PHP_VERSION, 5.4, '>=') ? E_ALL & ~E_DEPRECATED & ~E_NOTICE & ~E_STRICT : E_ALL & ~E_NOTICE);

/* code2 */
if(version_compare(PHP_VERSION,5.3,'>=')){
    $errors_to_log = E_ALL & ~E_DEPRECATED &~E_NOTICE;
}else if(version_compare(PHP_VERSION, 5.4, '>=')){
    $errors_to_log = E_ALL & ~E_DEPRECATED & ~E_NOTICE & ~E_STRICT;
}else{
    $errors_to_log = E_ALL & ~E_NOTICE;
}
?>
Was it helpful?

Solution

Is code 1 equal to code 2?

Yes. Code 1 uses nested ternary operator while code 2 uses the else-if structure. Code 1 would've been more clear if parentheses were used to show the precedence.

What do & and ~ mean?

& in php refers to BITWISE AND operator. ~ in php refers to BITWISE NOT or Complement operator. In this case, it gives the complement values of the constants.

However, you need to know that ~ has the highest precedence here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top