Home > Article > Backend Development > php comparison operators
Comparison operators, as their name implies, allow comparison of two values.
Comparison Operator
Example Name $a is equal to $b. $a === $b Congruent TRUE if $a is equal to $b and their types are the same. $a != $b Not equal TRUE, if $a is not equal to $b after type conversion.
$a a8093152e673feb7aba1828c43532094 $b does not equal TRUE, if $a does not equal $b after type conversion.
$a !== $b Not Congruent TRUE if $a is not equal to $b, or their types are different.
$a 959bcf89129168f4579e9617b4d2edd0 $b is greater than TRUE if $a is strictly greater than $b.
$a f7eb96d8dd8fc930932b762fcc6c609b= $b Greater than or equal to TRUE if $a is greater than or equal to $b.
If you compare a number and a string or compare strings involving numeric content, the string will be converted to a numeric value and the comparison will be performed as a numeric value. This rule also applies to switch statements. When comparing with === or !==, no type conversion is performed because both types and values are compared.
<?php var_dump(0 == "a"); // 0 == 0 -> true var_dump("1" == "01"); // 1 == 1 -> true var_dump("10" == "1e1"); // 10 == 10 -> true var_dump(100 == "1e2"); // 100 == 100 -> true switch ("a") { case 0: echo "0"; break; case "a": // never reached because "a" is already matched with 0 echo "a"; break; } ?>
For multiple types, comparison operators compare according to the following table (in order).
Compare multiple types
operand1 type
operand2 type
result
null or string string Convert NULL to "" for numeric or lexical comparison
bool or null any other Type Convert to bool, FALSE < TRUE
object Object Built-in classes can define their own comparisons, different classes cannot be compared, properties of the same class and arrays are compared in the same way (PHP 4), PHP 5 has its own description
string , resource or number string, resource or number Convert strings and resources to numbers, compared according to ordinary mathematics
array array Arrays with fewer members are smaller, if the key in operand 1 does not exist in operand 2 then Arrays cannot be compared, otherwise they will be compared one by one (see the example below)
object Any other type Object is always larger
array Any other type Array is always larger
Example #1 Standard array comparison code
<?php // 数组是用标准比较运算符这样比较的 function standard_array_compare($op1, $op2) { if (count($op1) < count($op2)) { return -1; // $op1 < $op2 } elseif (count($op1) > count($op2)) { return 1; // $op1 > $op2 } foreach ($op1 as $key => $val) { if (!array_key_exists($key, $op2)) { return null; // uncomparable } elseif ($val < $op2[$key]) { return -1; } elseif ($val > $op2[$key]) { return 1; } } return 0; // $op1 == $op2 } ?>
Warning: Due to The internal representation of floating point numbers float should not compare two floating point numbers for equality.
Ternary OperatorAnother conditional operator is the “?:” (or ternary) operator. Example #2 Assign default value<?php // Example usage for: Ternary Operator $action = (empty($_POST['action'])) ? 'default' : $_POST['action']; // The above is identical to this if/else statement if (empty($_POST['action'])) { $action = 'default'; } else { $action = $_POST['action']; } ?>
Since PHP 5.3, you can omit the middle part of the ternary operator. The expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE and expr3 otherwise.
Note: Note that the ternary operator is a statement, so its evaluation is not a variable, but the result of the statement. This is important if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a function that returns by reference will not work, and a future version of PHP will issue a warning about this.
Note:
It is recommended to avoid stacking ternary operators together. When multiple ternary operators are used in one statement, the results of PHP operations will be unclear:
Example #3 Unclear ternary operator behavior
<?php // 乍看起来下面的输出是 'true' echo (true?'true':false?'t':'f'); // 然而,上面语句的实际输出是't',因为三元运算符是从左往右计算的 // 下面是与上面等价的语句,但更清晰 echo ((true ? 'true' : 'false') ? 't' : 'f'); // here, you can see that the first expression is evaluated to 'true', which // in turn evaluates to (bool)true, thus returning the true branch of the // second ternary expression. ?>