Parsing PHP comparison and logical operators

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

As their name implies, two values are allowed to be compared. There are several comparison operators:
1)$a > $bGreater than: Returns True if the $a is strictly greater than $b
2)$a < $bLess than: Returns True if the $a is strictly less than $b
3)$a >= $bGreater than equals: Returns True if $a is greater than or equal to $b
4)$a <= $bLess than equals: Returns True if $a is less than or equal to $b
5)$a <> $bNot equal to: Returns True if $a is not equal to $b
6)$a! = $bNot equal to: Returns True if $a is not equal to $b (IBID.)
7)$a = = $bequals: Returns True if $a equals $b
8)$a = = = $bAll equals: Returns True if $a equals $b and their type is the same
9)$a!== $bNot all equals: 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 character strings, 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.
For example:
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 "", compare numbers or strings
bool or null comparison: Convert to Bool,false < TRUE
Object built-in classes can define their own comparisons, Different classes cannot be compared, and the same class compares the comparison between the property
String,resource or number: converting strings and resources to numbers, comparison by general math
: Arrays with fewer members are smaller if the keys in the operand 1 do not exist in the operand 2 There is no comparison between the arrays, 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_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 the comparison operator:

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.

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.