PHP Comparison Operators

Comparison operators like logical operators, provide a method to direct program flow through an examination of the comparative values of two or more variables.

Example Label Outcome
$a < $b Less than True if $a is less than $b
$a > $b Greater than True if $a is greater than $b
$a <= $b Less than or equal to True if $a is less than or equal to $b
$a >= $b Greater than or equal to True if $a is greater than or equal to $b
($a == 12) ? 5 : -1 Ternary If $a equals 12, return value is 5; otherwise, return value is -1

Note: that the comparison operators should be used only for comparing numerical values. Although you may be tempted to compare strings with these operators, you will most likely not arrive at the expected outcome if you do so. There is a substantial set of predefined functions that compare string values.





Content