Categories
PHP

Conditional Expressions

Comparison operators let you compare two expressions. If the comparison test is successful, the expression evaluates to true, and if the test fails, the expression evaluates to false. You often use comparison operators with conditional statements (e.g. if…else ) and loops (e.g. while loop, for loop, etc.). The logical operators work on Boolean values. You often use logical operators to combine the results of two (or more) of the comparison operators.

Comparison Operators

Comparison operators return the Boolean results of true or false.

OperatorName
==Equal$a == $b;
true if $a is equal to $b, false otherwise
!=Not Equal$a != $b;
true if $a is not equal to $b, false otherwise
<>Not Equal$a <> $b;
true if $a is not equal to $b, false otherwise
===IdenticalCompares equality and data type of both expressions.
$a === $b;
true if $a is equal to $b, and both ($a and $b) have the same data types, false otherwise.
!==Not identical$a !== $b;
true if $a is not equal to $b, or both ($a and $b) have the different data types, false otherwise.
<Less than$a < $b;
true if $a is less than to $b, false otherwise
>Greater than$a > $b;
true if a$ is greater than to $b, false otherwise
<=Less than or equal to$a <= $b;
true if $a is less than or equal to $b, false otherwise
>=Greater than or equal$a >= $b;
true if $a is greater than or equal to $b, false otherwise
List of comparison operators in PHP

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:

<?php
 $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:

<?php
 $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.

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

<?php
 $var = 5;
 // 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";

The identical operator ===: This operator 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:

<?php
 // 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";

 $a = 1; $b = '1';
 if ($a !== $b)
  echo '$a and $b are not identical';
 if ($a === $b)
  echo '$a and $b are identical';

The conditional expressions described here can compare strings but usually not with the expected results. If strings need to be compared use the function strcmp( ).

Logical Operators

A logical operator is used to combine the results of two (or more) of the comparison operators. In simple words, if something has a TRUE or FALSE value, it can be used with a logical operator. A logical operator takes two true or false inputs and produces a true or false result:

Operator
&&$a && $b;
true if both $a and $b evaluate to true, false otherwise
and$a and $b;
true if both $a and $b evaluate to true, false otherwise
||$a || $b;
true if either $a or $b evaluates to true, false otherwise
or$a or $b;
true if either $a or $b evaluates to true, false otherwise
xor$a xor $b;
true if $a or $b evaluates to true
false if both $a and $b evaluates to true
false if both $a and $b evaluates to false
!!$a; (Not operator)
true if $a is false, false if $a is true
List of logical operators in PHP

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:

<?php
 $var = 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:

<?php
 $var = 2;
 $var = 6;
 if ($var == 2) && ($var2 == 6)
  echo "The variables are equal to 2 and 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.

Short Circuit Evaluation: PHP skips the evaluation of some expressions in a logical expression when the value of the expression is determined.

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.

<?php
 $var = 6;
 $var2 = 7;
 if ((($var == 6) && ($var2 == 7)) ||
   (($var == 4) && ($var2 == 1)))
  echo "Expression is true";

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

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:

<?php
$var = 1;
if (!($var != 1))
  echo "variable is one";
if ($var == 1)
  echo "variable is one";

So are the following:

<?php
$var = 1;
if ($var < 10)
  echo "less than 10";
if (!($var >= 10))
  echo "less than 10";

Probably the most common use of the unary not operator is to check if a function call fails, and we often use this with the database functions.


PHP Control Structures:

  1. If elseif else and Switch Case Conditional Statements
  2. Conditional Expressions
  3. Ternary, Elvis, and Null Coalescing Operators
  4. Using Loops
  5. Using GOTO Operator