Table 1. PHP comparison Operators
Example name result
$a = = $b equals TRUE If the $a equals $b.
$a = = = $b congruent TRUE if $a equals $b, and they are of the same type. (introduced in PHP 4)
$a! = $b unequal to TRUE if $a is not equal to $b.
$a <> $b Not equal to 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. (PHP 4 only)
$a < $b small with TRUE if the $a is strictly less than $b.
$a > $b is greater than TRUE if $a strictly $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. If you compare two numeric strings, they are compared as integers. This rule also applies to switch statements.
- < ? PHP
- var_dump (00 = = 0-> true
- 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 operands are of different types, they are compared (in order) according to the following table.
Table 2. PHP comparison operators compare different types
Number of operands 1 type operand 1 type result
Null or string string converts null to "" for numeric or lexical comparisons
BOOL or null any other type conversion to Bool,false < TRUE
The object object built-in class can define its own comparison, not comparable to the same class and arrays in the same way to compare attributes (PHP 4), PHP 5 has its own description
String,resource or number String,resource or numbers converts strings and resources to numerals, compared by normal math
An array with fewer members is smaller if the key in the operand 1 does not exist in the operand 2 and 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 larger
Example 1. Standard array Comparison code
- < ? PHP
- Arrays are compared with standard comparison operators.
- function Standard_array_compare ($op 1, $op 2)
- {
- if (count ($op 1) < count($op 2)) {
- 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;
- }
- }
- return 0;//$ OP1 = = $op 2
- }
- ?>
Ternary operators of PHP comparison operators
Another conditional operator is the "?:" (or ternary) operator. Example 2. Assigning default values
- < ? PHP
- Example usage for:ternary Operator
- $ Action
? ' Default ': $_post[' action ';
This if/else statement
- if (Empty ($_post[' action '])) {
- $ Action ' default ';
- } else {
- $ Action = $_post[' action '];
- }
- ?>
An 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: Note 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.
http://www.bkjia.com/PHPjc/446050.html www.bkjia.com true http://www.bkjia.com/PHPjc/446050.html techarticle table 1. The PHP comparison operator 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. (PH ...