Comparison operator types

As their name implies,Allows comparison of two values.There are several comparison operators:

1) $a>$b is greater than:Returns true if $a is strictly greater than $b

2) $a<$b less than:true if $a is strictly less than $b

3) $a>= $b greater than or equal to:true if $a is greater than or equal to $b

4) $a<= $b less than or equal to:true if $a is less than or equal to $b

5) $a<$b is not equal:true if $a is not equal to $b

6) $a!=$B is not equal:if $a is not equal to $b, return true (same as above)

7) $a == $b is equal to:if $a is equal to $b, return true

8) $a === $b are all equal:if $a is equal to $b and their types are the same,Return true

9) $a! == $b are not all equal:if $a is not equal to $b, or their types are different,Return true

Among them, we have to distinguish between "equal" and "all equal". $A == $b is just a comparison operation of the values ​​of two variablesAnd all is equal to the value comparison and data type comparison of the expressions on both sides of the operator,Only the values ​​on both sides are equal,The result of the operation is "true". In conjunction with the "inequality" operator, for example,$a=2;var_dump ($a! == 2);This expression returns "false" because 2 is equal to 2. In addition, $a=2;is an integer and var_dump ($a! == 2);is also an integer.But the operator is not all equal to "! ==" so the result is false,Because 2 is equal to 2. Conversely, if this is the case $a=2;var_dump ($a! == "2");The result of the operation is "true", because 2 is not equal to "2", and the following "2" is a string "2 ", Which means not just comparing variable values,The data types of the variables are also compared.

Comparing different types of results in php

If the php comparison operator compares an integer and a string,The strings are then converted to integers and compared.If you compare two numeric strings,Will compare them as integers,This rule also applies to switch statements.

such as:

var_dump (0 == "a");//returns true, "a" is converted to 0

var_dump ("1" == "01");//returns true, treated as an integer

string or null and string comparison:converts null to "" and compares numbers or strings

Comparison between bool or null:converted to bool, false<true

The object built-in class can define its own comparison,Different classes cannot be compared,Compare properties for the same class

comparison between string, resource or number:converting strings and resources into numbers,Compare by ordinary mathematics

Comparison between arrays:arrays with fewer members are smaller,If the keys in operand 1 do not exist in operand 2, the arrays cannot be compared.Need to compare value by value (see code below)

array compared to any other type:array is always bigger

object compared to any other type:object is always bigger

Array comparison code:

function standard_array_compare ($op1, $op2)
   if (count ($op1)<count ($op2)) {//array with fewer members is smaller
     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;
    } else if ($val<$op2 [$key]) {
       return -1;
     } elseif ($val>$op2 [$key]) {
       return 1;
   return 0;//$op1 == $op2

Ternary operator in comparison operators:

The expression (expr1)?(expr2):(expr3) When the value of the expression expr1 is true, the value is expr2, and when the value of the expression expr1 is false, the value is expr3.

  • Previous js force URL to default homepage
  • Next Analysis of the use of Java exception handling assertions