Conditional Expressions

The most common conditional comparison is to test the equality of two expressions with the Boolean result of true or false. Equality is tested with the double-equal operator, ==. Consider an example:

$var = 1;
if ($var == 1)
echo "Equals one!";

If $var is equal to 1, the example evaluates as true and prints the message. If the example evaluates as false, nothing is printed.

Inequality can be tested with the != inequality operator:

$var = 0;
if ($var != 1)
echo "Does not equal one!";

This evaluates as true and prints the message if $var isn't equal to 1. The operator != is usually referred to as the not equals operator, because the exclamation mark character negates an equality expression.

If the equality operator == and the assignment operator = are unfamiliar beware: they are easy to inadvertently interchange. This is a very common bug and hard to detect.

The incorrectly formed conditional expression ($var=1) always evaluates as true, because the assignment that actually occurs always succeeds and, therefore, is always true.

The error of incorrectly replacing an assignment with == is a far less common mistake. However, it's also difficult to detect because an incorrectly written assignment of $var==1; is quietly evaluated as true or false with no effect on $var.

Expressions can be combined with parentheses and with the Boolean operators && (and) and || (or). For example, the following expression returns true and prints the message if $var is equal to either 3 or 7:

if ($var == 3) || ($var == 7)
echo "Equals 3 or 7";

The following expression returns true and prints the message if $var equals 2 and $var2 equals 6:

Interestingly, if the first part of the expression ($var==2) evaluates as false, PHP doesn't evaluate the second part of the expression ($var2==6), because the overall expression can never be true; both conditions must be true for an && (and) operation to be true. This short-circuit evaluation property has implications for design; to speed code, write the expression most likely to evaluate as false as the left-most expression, and ensure that computationally expensive operations are as right-most as possible.

Never assume that expressions combined with the Boolean operators && and || are evaluated. PHP uses short-circuit evaluation when determining the result of a Boolean expression.

More complex expressions can be formed through combinations of the Boolean operators and the liberal use of parentheses. For example, the following expression evaluates as true and prints the message if one of the following is true: $var equals 6 and $var2 equals 7, or $var equals 4 and $var2 equals 1.

As in assignment expressions, parentheses ensure that evaluation occurs in the required order.

Equality and inequality are the two basic comparisons, but numbers are also compared to determine which is greater or lesser. Consider the following examples:

// Returns true if $var is less than 5
if ($var < 5)
echo "Less than 5";
// Returns true if $var is less than or equal to 5
if ($var <= 5)
echo "Less than or equal to 5";
// Returns true if $var is greater than 5
if ($var > 5)
echo "Larger than 5";
// Returns true if $var is greater than or equal to 5
if ($var >= 5)
echo "Equal to or larger than 5";

There is a new operator in PHP4, the is-identical operator ===. This isn't found in other languages and returns true only if the expression evaluates as equal and the arguments are of the same type. Consider an example:

// Returns true, since both are integers and equal
if (5 === 5)
echo "Same types and value";
// Returns false, since there are mixed types
// (5.0 is a float, and 5 is an integer)
if (5.0 === 5)
echo "This never prints!";
// The normal equality check would return true
if (5.0 == 5)
echo "This always prints";

The conditional expressions described here can compare strings but usually not with the expected results. If strings need to be compared-a common requirement-use the PHP string library function strcmp( ).

Any of the Boolean expressions we have discussed can be negated with an exclamation mark !, the unary not operator. The following two expressions are equivalent: