If you compare a number with a string or a string that involves the content of a number, the string is converted to a value and compared to a value. This rule also applies to switch statements. When used or! During the comparison, no type conversion is performed because the types and values must be compared. Comparison operators, as their names imply, allow comparison of two values.
Comparison Operators
Example name result
$ A = $ B is equal to TRUE. if $ a is equal to $ B after type conversion.
$ A ===$ B all equal to TRUE, if $ a is equal to $ B, and their types are the same.
$! = $ B is not equal to TRUE. if $ a is not equal to $ B after type conversion.
$ A <> $ B is not equal to TRUE. if $ a is not equal to $ B after type conversion.
$! = $ B is not all TRUE, if $ a is not equal to $ B, or they are of different types.
$ A <$ B small and TRUE, if $ a is strictly less than $ B.
$ A> $ B is greater than TRUE if $ a is strictly greater than $ B.
$ A <= $ B is 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 a number with a string or a string that involves the content of a number, the string is converted to a value and compared to a value. This rule also applies to switch statements. When = or! = During comparison, no type conversion is performed, because the types and values must be compared.
truevar_dump("1" == "01"); // 1 == 1 -> truevar_dump("10" == "1e1"); // 10 == 10 -> truevar_dump(100 == "1e2"); // 100 == 100 -> 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 according to the following table (in order ).
Compare multiple types
Operation Number 1 type
Operation count 2 type
Result
Null or string converts NULL to "" for comparison of numbers or words
Bool or null any other type is converted to bool, FALSE <TRUE
Object built-in classes can define their own comparisons. different classes cannot be compared. the same classes and arrays are used to compare attributes (in PHP 4). PHP 5 has its own instructions.
String, resource, or number string, resource, or number converts a string and a resource to a number.
Array has a small array of fewer members. if the key in Operation Number 1 does not exist in Operation Number 2, the array cannot be compared; otherwise, the values are compared one by one (see the following example)
Any other object type of the object is always larger.
Array any other type of array is always larger
Example #1 standard array comparison code
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: because of the internal expression of floating point float, it is not necessary to compare whether two floating point numbers are equal.
Ternary operators
Another conditional operator is "? : "(Or ternary) operator.
Example #2 assign the default value
Expression (expr1 )? (Expr2): (expr3) when the value of expr1 is TRUE, the value is expr2, and when the value of expr1 is FALSE, the value is expr3.
From PHP 5.3, the portion in the middle of the ternary operator can be omitted. Expression expr1? : Expr3 returns expr1 when the value of expr1 is TRUE; otherwise, expr3 is returned.
Note: the ternary operator is a statement. Therefore, the result is not a variable but a statement. It is important to return a variable through reference. Return $ var = 42? $ A: $ B; will not work, and a warning will be issued for future PHP versions.
Note:
We recommend that you do not use the ternary operators together. When multiple ternary operators are used in a statement, the results of the PHP operation are unclear:
Example #3 unclear behavior of ternary operators