Logical Operators, Associativity and Precedence in PHP

PHP supports the basic if, if-else, and if-else-if conditional structures as well as switch statements. Logical statements are built using the standard logical operators for numeric comparisons as well as logical operators such as negations, And, and Or.

Logical Operators

PHP has a built-in Boolean type and supports the keywords true and false . However, any variable can be treated as a Boolean if used in a logical expression. Depending on the variable, it could evaluate to true or false! For example, an empty string, "", null, or a numeric value of zero, 0 are all considered false. A non-empty string, a non-zero numeric value, or a non-empty array all evaluate to true. It is best to avoid these issues by writing clean code that uses clear, explicit statements. Because PHP is dynamically typed, comparison operators work differently depending on how they are used. First, let’s consider the four basic inequality operators, <, <=, >, and >=. When used to compare numeric types to numeric types, these operators work as expected and the value of the numbers are compared.

$a = 10;
$b = 20;
$c = 20;

$r = ($a < $b); //true
$r = ($a <= $b); //false
$r = ($b <= $c); //true
$r = ($a > $b); //false
$r = ($a >= $b); //false
$r = ($b >= $c); //true

When these operators are used to compare strings to strings, the strings are compared lexicographically according to the standard ASCII text table. Some examples follow, but it is better to use a function to do string comparisons.

$s = "aardvark";
$t = "zebra";

$r = ($s < $t); //true
$r = ($s <= $t); //true
$r = ($s >= $t); //false
$r = ($s > $t); //false

However, when these operators are used to compare strings to numeric types, the strings are converted to numbers using the same type juggling that happens when strings are mixed with arithmetic operators. In the following example, $b gets converted to a numeric type when compared to $a which give the results indicated in the comments.

$a = 10;
$b = "10";

$r = ($a <= $b); //true
$r = ($a < $b); //false
$r = ($a >= $b); //true
$r = ($a > $b); //false

With the equality operators, == and !=, something similar happens. When the types of the two operands match, the expected comparison is made: when numbers are compared to numbers their values are compared; when strings are compared to strings, their content is compared (case sensitively). However, when the types are different they are type juggled and strings are converted to numbers for the purpose of comparison. Thus, a comparison like (10 == "10") ends up being true! The operators are == and != are referred to as loose comparison operators because of this. What if we want to ensure that we’re comparing apples to apples? To rectify this, PHP offers another set of comparison operators, strict comparison operators,=== and !== (each has an extra equals sign). These operators will make a comparison without type casting either operand first. Now a similar comparison, (10 === "10") ends up evaluating to false. The operator === will only evaluate to true if the both the operands’ type and value are the same. This is similar to how the == and === behave in JavaScript.

$a = 10;
$b = "10";

$r = ($a == $b); //true
$r = ($a != $b); //false
$r = ($a === $b); //false
$r = ($a !== $b); //true

The three basic logical operators, not !, and &&, and or || are also supported in PHP.

Order of Precedence

At this point it is worth summarizing the order of precedence of all the operators that we’ve seen so far including assignment, arithmetic, comparison, and logical. Since all of these operators could be used in one statement, for example:

($b*$b < 4*$a*$c || $a === 0 || $argc != 4)

it is important to understand the order in which each one gets evaluated. The table below summarizes the order of precedence for the operators seen so far. This is not an exhaustive list of PHP operators.

Operator Order of Precedence in PHP. Operators on the same level have equivalent order and are performed in the associative order specified.

Operator Order of Precedence in PHP. Operators on the same level have equivalent order and are performed in the associative order specified.


Licenses and Attributions


Speak Your Mind