Table 1. PHP comparison Operators
Example name result
$ A = $ B is equal to TRUE if $ a is equal to $ B.
$ A ===$ B all equal to TRUE, if $ a is equal to $ B, and their types are the same. (Introduced in PHP 4)
$! = $ B is not equal to TRUE, if $ a is not equal to $ B.
$ A <> $ B is not equal to TRUE, if $ a is not equal to $ B.
$! = $ B is not equal to TRUE. If $ a is not equal to $ B, or they have different types. (PHP 4 only)
$ A <$ B Small and TRUE, if $ a is strictly less than $ B.
$ A> $ B is greater than TRUE if $ a is strict with $ 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 the PHP comparison operator compares an integer and a string, the string is converted to an integer. Compare two numeric strings as integers. This rule also applies to switch statements.
- < ?php
- var_dump(0 == "a"); // 0 == 0 -> true
- var_dump("1" == "01"); // 1 == 1 -> true
- switch ("a") {
- case 0:
- echo "0";
- break;
- case "a": // never reached because "a" is already matched with 0
- echo "a";
- break;
- }
- ?>
If the operation numbers are of different types, compare them according to the following table (in order ).
Table 2. Comparison operators in PHP compare different types
Operation count 1 operation count 1 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)
Array any other type of array is always larger
Any other object type of the object is always larger.
Example 1. Standard array comparison code
- <? Php
- // Arrays are compared using standard comparison operators.
- 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
- }
- ?>
Comparison operators in PHP
Another conditional operator is "? : "(Or ternary) operator. Example 2. Assign the 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'];
- }
- ?>
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.
Note: The ternary operator is a statement, so its evaluation is not a variable, but the result of the 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.