Comparison Operators
Example |
Name |
Result |
$ A = $ B |
Equal |
TrueIf$Is equal$ B. |
$ A ===$ B |
Identical |
TrueIf$Is equal$ B, And they are of the same type. (introduced in PHP 4) |
$! = $ B |
Not equal |
TrueIf$Is not equal$ B. |
$ A <> $ B |
Not equal |
TrueIf$Is not equal$ B. |
$! ==$ B |
Not identical |
TrueIf$Is not equal$ B, Or they are not of the same type. (introduced in PHP 4) |
$ A <$ B |
Less |
TrueIf$Is strictly less$ B. |
$ A> $ B |
Greater |
TrueIf$Is strictly greater$ B. |
$ A <= $ B |
Less than or equal |
TrueIf$Is less than or equal$ B. |
$ A> = $ B |
Greater than or equal |
TrueIf$Is greater than or equal$ B. |
Note: If you compare a string with an integer value. String is converted to the number type (integer or float type), and then compared with integer.
1 <? PHP
2 Var_dump ( 0 = " A " ); // 0 = 0-> true
3 Var_dump ( " 1 " = " 01 " ); // 1 = 1-> true
4 Var_dump ( " 1 " = " 1e0 " ); // 1 = 1-> true
5
6 Switch ( " A " ){
7 Case 0 :
8 Echo " 0 " ;
9 Break ;
10 Case " A " : // Never reached because "A" is already matched with 0
11 Echo " A " ;
12 Break ;
13 }
14 ?>
15
COMPARISON WITH VARIOUS TYPES
Type of operand 1 |
Type of operand 2 |
Result |
Null or string |
String |
ConvertNullTo "", numerical or lexical comparison |
Bool or null |
Anything |
Convert to bool,False<True |
Object |
Object |
Built-in classes can define its own comparison, different classes are uncomparable, same class-compare properties the same way as arrays (PHP 4), PHP 5 has its own explanation |
String, resource or number |
String, resource or number |
Translate strings and resources to numbers, usual math |
Array |
Array |
Array with fewer members is smaller, if key from operand 1 is not found in operand 2 then arrays are uncomparable, otherwise-compare value by value (see following example) |
Array |
Anything |
Array is always greater |
Object |
Anything |
Object is always greater |
When comparing two numbers, we 'd better understand all the meanings in this table first. Yes.
I once suffered losses here.
In addition, when comparing two values, if you can use "=" and "! The = "operator uses the two operators. This prevents PHP from automatically converting data types.