Types of PHP comparison operators

Source: Internet
Author: User
This article mainly introduces the types of PHP comparison operators, interested in the reference of friends, I hope to be helpful to everyone.

comparison operator types

As their name implies, two values are allowed to be compared. There are several comparison operators:
1) $a > $b 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 equals: Returns True if $a is greater than or equal to $b
4) $a <= $b less than equals: Returns True if $a is less than or equal to $b
5) $a <> $b not equal to: Returns True if $a is not equal to $b
6) $a! = $b does not equal: Returns True if the $a is not equal to $b (IBID.)
7) $a = = $b equals: Returns True if $a equals $b
8) $a = = = $b All equals: Returns True if $a equals $b and their type is the same
9) $a!== $b not equal to: Returns True if $a are not equal to $b, or if they are of different types

Among them, we should focus on the distinction between "equals" and "all equals", $a = = $b Only two variables of the value of the comparison operation, and all equal to the operator on both sides of the expression at the same time the value comparison and data type comparison, only the two sides of the value are equal, the result of the operation is "true". Combining 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, is integral and var_dump ($a!==2), 2 is the integer type, but the operator is not equal to "! = = "So the result is false, because 2 is equal to 2." In turn if so $ A = 2; Var_dump ($a!== ' 2 '); The result of the operation is "true", because 2 is not equal to ' 2 ', and the subsequent ' 2 ' is a string ' 2 ', i.e. not all equals not just the value of the comparison variable, but also the data type of the variable.


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 after the comparison. If you compare two numeric strings, they are compared as integers, and this rule applies to the switch statement as well.
Like what:
Var_dump (0 = = "a"); Returns True, "a" is converted to 0
Var_dump ("1" = = "01"); Returns true As Integer processing

string or null and string comparison: convert null to "" for numeric or string comparisons
Comparison between bool or null: Convert to Bool,false < TRUE
Object built-in classes can define their own comparisons, not comparable, the same class is the comparison property
Comparison between String,resource or number: converts strings and resources to numbers, compared by ordinary mathematics
Comparison between arrays: Smaller array with fewer members, if the key in operand 1 does not exist in the 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 compared to any other type: object is always bigger


Array Comparison code:

function Standard_array_compare ($op 1, $op 2)  {    if (count ($op 1) < count ($op 2)) {    //small     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 null ;        } 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 when the value is EXPR2, when the value of the expression Expr1 is FALSE, the value is EXPR3.

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

PHP Data Export Case Study

Php method for generating PDF files

PHP six ways to sort arrays

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.