PHP comparison operators and symbols for logical operations

Source: Internet
Author: User
An operator is a symbol that tells the compiler to perform a particular arithmetic or logical operation, and usually forms an expression along with the operand, which we often see as participating in mathematical operations or logical operations. PHP also contains a lot of operators, this article in detail to understand the important comparison operators.

comparison operator types

The

is allowed to compare two values as implied by their name. The comparison operators are as follows:
1) $a > $b greater than: Returns True if the $a is strictly greater than $b
2) $a < $b less than: true
3) $a >= $a greater than or equal if the $b is strictly less than $b: if $a is greater than or equal to $b, returns true
4) $a <= $b less than equals: if $a is less than or equal to $b, true
5) $a <> $b not equal to: if $a is not equal to $b, returns true
6) $a! = $b Unequal To: Returns True if the $a is not equal to $b (IBID.)
7) $a = = $b equals: True
8 if $a equals $b) $a = = = $b equals: If $a equals $b and their type is the same, returns true
9) $a !== $b Not equal to: if $a is not equal to $b, or if their type is different, returns true

where we want to focus on distinguishing between "equals" and "all equals", $a = = $b just compares the values of two variables, and all equal to the expression on both sides of the operator at the same time the value comparison and data type comparison, only the two sides of the values 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)) {//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_k Ey_exists ($key, $op 2)) {return NULL,} else if ($val < $op 2[$key]) {return-1;} elseif ($val > $op 2[$key]) {Retu RN 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.

  • Related Article

    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.