A detailed introduction to PHP comparison operators _php tips

Source: Internet
Author: User

comparison operator types

As their name implies, two values are allowed to be compared. Comparison operators have the following:
1 $a > $b is greater than: Returns True if the $a is strictly greater than $b
2 $a < $b less than: Returns True if the $a is strictly less than $b
3 $a >= $b greater than or equal to: Returns True if $a is greater than or equal to $b
4 $a <= $b less than or equal to: Returns True if $a is less than or equal to $b
5 $a <> $b is not equal to: Returns True if $a is not equal to $b
6 $a!= $b is not equal to: Returns True if $a is not equal to $b (IBID.)
7 $a = = $b equals: Returns True if $a equals $b
8) $a = = = $b equals: Returns True if $a equals $b and their types are the same
9 $a!== $b is not equal to: Returns True if $a is not equal to $b, or if their type is different

Among them, we should focus on the distinction between "equals" and "total equals", $a = = $b only compares the values of two variables, all equal to the comparison of values between the two expressions on both sides of the operator and the comparison of data types, only the values on both sides are equal and the result of the operation is true. Combined with the "not congruent" operator, for example, $a = 2; Var_dump ($a!==2); The return value of this expression is "false" because 2 is equal to 2. In addition, the $a = 2; it is integral and var_dump ($a!==2); 2 is integral, but the operator is not all equal! = "So the result is false, because 2 is equal to 2." Conversely if this is so $a = 2; Var_dump ($a!== ' 2 '); The result of the operation is "true" because 2 is not equal to ' 2 ', and the ' 2 ' is a string ' 2 ', which is not equal to not just comparing variable values, but also comparing the data types of variables.


compare different types of results in PHP

If the PHP comparison operator compares an integer and a string, the string is converted to an integer and then compared. If you compare two numeric strings, they are compared as integers, and this rule applies to switch statements as well.
Like what:
Var_dump (0 = "a"); Returns True, "a" is converted to 0
Var_dump ("1" = = "01"); Returns true As Integer processing

Comparison of string or null and string: Convert null to "" and compare numbers or strings
Comparison between bool or null: Convert to Bool,false < TRUE
The object built-in class can define its own comparisons, not the same kind cannot be compared, and the same class compares attributes
Comparison between String,resource or number: converts strings and resources to numbers, compared to ordinary mathematics
Comparison between array: Smaller arrays with fewer members, if the key in operand 1 does not exist in operand 2, the array cannot be compared, and a value comparison is required (see code below).
Array and any other type comparison: array is always larger
Object and any other type comparison: object is always larger


Array Comparison code:

 function Standard_array_compare ($op 1, $op 2) 
 { 
   if (count ($op 1) < count ($op 2)) {    //smaller
     array with fewer members 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 
       nul l;    
    } else if ($val < $op 2[$key]) { 
       return-1 
     } elseif ($val > $op 2[$key]) {return 
       1; 
      } 
   } return 
   0;   $op 1 = $op 2 
 } 

Ternary operators in comparison operators:

An 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.

The above is the focus of the comparison operators, the text of these operators and comparison rules are analyzed, the following will be through more practice to master and digest the content.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.