Read more about PHP comparison operators _php tutorial

Source: Internet
Author: User

Table 1. PHP comparison Operators

Example name result
$a = = $b equals TRUE If the $a equals $b.
$a = = = $b congruent TRUE if $a equals $b, and they are of the same type. (introduced in PHP 4)
$a! = $b unequal to TRUE if $a is not equal to $b.
$a <> $b Not equal to TRUE if $a is not equivalent to $b.
$a!== $b non-congruent TRUE if $a are not equal to $b, or they are of different types. (PHP 4 only)
$a < $b small with TRUE if the $a is strictly less than $b.
$a > $b is greater than TRUE if $a strictly $b.
$a <= $b is less than or equal to TRUE if $a is less than or equal to $b.
$a >= $b is greater than or equal to TRUE if $a is greater than or equal to $b.

If the PHP comparison operator compares an integer and a string, the string is converted to an integer. If you compare two numeric strings, they are compared as integers. This rule also applies to switch statements.

 
  
  
  1. < ? PHP
  2. var_dump (00 = = 0-> true
  3. 1 = = 1- > true
  4. Switch ("a") {
  5. Case 0:
  6. echo "0";
  7. Break
  8. Case ' a ':///Never reached because "a" is already matched with 0
  9. echo "a";
  10. Break
  11. }
  12. ?>

If the operands are of different types, they are compared (in order) according to the following table.

Table 2. PHP comparison operators compare different types

Number of operands 1 type operand 1 type result
Null or string string converts null to "" for numeric or lexical comparisons
BOOL or null any other type conversion to Bool,false < TRUE
The object object built-in class can define its own comparison, not comparable to the same class and arrays in the same way to compare attributes (PHP 4), PHP 5 has its own description

String,resource or number String,resource or numbers converts strings and resources to numerals, compared by normal math
An array with fewer members is smaller if the key in the operand 1 does not exist in the operand 2 and the array cannot be compared, otherwise the values are compared (see example below)

Array any other type array is always larger
Object any other type object is always larger

Example 1. Standard array Comparison code

 
 
  1. < ? PHP
  2. Arrays are compared with standard comparison operators.
  3. function Standard_array_compare ($op 1, $op 2)
  4. {
  5. if (count ($op 1) < count($op 2)) {
  6. return-1;//$OP 1 < $op 2
  7. } elseif (Count ($op 1) > count ($op 2)) {
  8. return 1;//$OP 1 > $op 2
  9. }
  10. foreach ($op 1 as $key => $val) {
  11. if (!array_key_exists ($key, $op 2)) {
  12. return null; Uncomparable
  13. } elseif ($val < $op 2[$key]) {
  14. return-1;
  15. } elseif ($val > $op 2[$key]) {
  16. return 1;
  17. }
  18. }
  19. return 0;//$ OP1 = = $op 2
  20. }
  21. ?>

Ternary operators of PHP comparison operators

Another conditional operator is the "?:" (or ternary) operator. Example 2. Assigning default values

 
  
  
  1. < ? PHP
  2. Example usage for:ternary Operator
  3. $ Action
    ? ' Default ': $_post[' action ';

  4. This if/else statement
  5. if (Empty ($_post[' action '])) {
  6. $ Action ' default ';
  7. } else {
  8. $ Action = $_post[' action '];
  9. }
  10. ?>

An expression (EXPR1)? (EXPR2): (EXPR3) when EXPR1 evaluates to TRUE, the value is EXPR2, and the value is EXPR1 when the EXPR3 evaluates to FALSE.

Note: Note that the ternary operator is a statement, so its evaluation is not a variable, but rather a result of the statement. This is important if you want to return a variable by reference. In a function returned by reference, return $var = = 42? $a: $b; Will not work, a warning will be issued for future versions of PHP.


http://www.bkjia.com/PHPjc/446050.html www.bkjia.com true http://www.bkjia.com/PHPjc/446050.html techarticle table 1. The PHP comparison operator example name results $a = = $b equals TRUE if $a equals $b. $a = = = $b congruent TRUE if $a equals $b, and they are of the same type. (PH ...

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