Comparison operators, as implied by their names, allow two values to be compared. You can also refer to the PHP type comparison table to see examples of different types of comparisons.
comparison Operators
Example |
name |
Results |
$a = = $b |
equals |
TRUE if $a equals $b. |
$a = = = $b |
congruent |
TRUE if $a equals $b, and they are of the same type. |
$a! = $b |
Not equal |
TRUE if $a is not equivalent to $b. |
$a <> $b |
Not equal |
TRUE if $a is not equivalent to $b. |
$a!== $b |
non-congruent |
TRUE If $a are not equal to $b, or they are of different types. |
$a < $b |
small with |
TRUE if $a is strictly less than $b. |
$a > $b |
is greater than |
TRUE if $a strictly $b. |
$a <= $b |
less than or equal to |
TRUE if $a is less than or equal to $b. |
$a >= $b |
is greater than or equal to |
TRUE if $a is greater than or equal to $b. |
If you compare an integer and a string, the string is converted to an integer. If you compare two numeric strings, they are compared as integers. This rule also applies to switch statements.
<?phpvar_dump (0 = = "a"); 0 = = 0-Truevar_dump ("1" = = "01"); 1 = = 1-truevar_dump ("1" = = "1e0"); 1 = = 1 Trueswitch ("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 are compared (in order) according to the following table.
Compare multiple Types
Number of operations 1 types |
Number of operations 1 types |
Results |
Null or String |
String |
Convert NULL to "" for numeric or lexical comparisons |
BOOL or NULL |
Any other type |
Convert to Bool,false < TRUE |
Object |
Object |
Built-in classes can define their own comparisons, non-homogeneous cannot be compared, the same class and arrays are the same way to compare properties (PHP 4), PHP 5 has its own description |
String,resource or number |
String,resource or number |
Converts strings and resources to numbers, compared by ordinary mathematics |
Array |
Array |
Arrays with fewer members are smaller if the keys in the operand 1 do not exist in the operand 2, the array cannot be compared, otherwise the values are compared (see example below) |
Array |
Any other type |
Array is always larger |
Object |
Any other type |
object is always bigger |
Example #1 Standard Array Comparison code
The <?php//array is a function standard_array_compare ($op 1, $op 2) { if (count ($op 1) < count ($op 2)) that is compared with the standard comparison operator { return-1; $op 1 < $op 2 } elseif (Count ($op 1) > Count ($op 2)) { return 1;//$OP 1 > $op 2 } foreach ($op 1 as $key = $val) { if (!array_key_exists ($key, $op 2)) { return null;//Uncomparable } elseif ($val < $op 2 [$key]) { return-1; } ElseIf ($val > $op 2[$key]) { return 1; } }
Ternary operators
Another conditional operator is the "?:" (or ternary) operator.
Example #2 Assigning default values
<?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 ']; }
?> expression (EXPR1)? (EXPR2): (EXPR3) when EXPR1 evaluates to TRUE, the value is EXPR2, and the value is EXPR1 when the EXPR3 evaluates to FALSE.
Note: Notice that the ternary operator is a statement, so its evaluation is not a variable, but rather a result of the statement. This is important if you want to return a variable by reference. In a function returned by reference, return $var = = 42? $a: $b; Will not work, a warning will be issued for future versions of PHP.
Note:
It is recommended that you avoid stacking ternary operators together. When using more than one ternary operator in a single statement, the result of PHP operation is not clear: IS is recommended so you avoid "stacking" ternary expressions. PHP ' s behaviour when using the more than one ternary operator within a single statement is non-obvious:
Example #3 unclear ternary operator behavior
<?php//at first glance it looks like the following output is ' True ' echo (true? ') True ': false? ' T ': ' f ');//However, the actual output of the above statement is ' t ' because the ternary operator is computed from left to right//below is the equivalent statement above, but clearer echo (true? ' True ': ' false ')? ' t ': ' f ');//Here, you can see that the first expression was evaluated to ' true ', which//-turn evaluates to (BOOL) True, Thus returning the true branch of the//second ternary expression.? >
Http://www.xici.net/d229813041.htm
Http://www.xici.net/d229813962.htm
Http://www.xici.net/d229814020.htm
Http://www.xici.net/d229814125.htm
PHP comparison Operators