Smartlab Software logo

PHP Precedence

I was working on an issue similar to this:

<?php
// BB not defined
define('AA', true);
$bTest = AA AND defined('BB');
?>

The problem was $bTest was true instead of false. The problem is AND has a lower precedence than '=' so AA was assigned to $bTest, which was true.

To fix this either use && instead of AND, which has a higher precedence than = or put parens around:

$bTest = (AA AND defined('BB'));

This will work correctly:

if(AA AND defined('BB')) {
  echo "wont get here";
}

Comments

Add a comment