PHP comparison Operators

Source: Internet
Author: User
Tags php compare

Table 1. PHP comparison Operators

Example name result
$ A = $ B is equal to TRUE if $ a is equal to $ B.
$ A ===$ B all equal to TRUE, if $ a is equal to $ B, and their types are the same. (Introduced in PHP 4)
$! = $ B is not equal to TRUE, if $ a is not equal to $ B.
$ A <> $ B is not equal to TRUE, if $ a is not equal to $ B.
$! = $ B is not equal to TRUE. If $ a is not equal to $ B, or they have different types. (PHP 4 only)
$ A <$ B Small and TRUE, if $ a is strictly less than $ B.
$ A> $ B is greater than TRUE if $ a is strict with $ 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. Compare two numeric strings as integers. This rule also applies to switch statements.

 
 
  1. < ?php  
  2. var_dump(0 == "a"); // 0 == 0 -> true  
  3. var_dump("1" == "01"); // 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 operation numbers are of different types, compare them according to the following table (in order ).

Table 2. Comparison operators in PHP compare different types

Operation count 1 operation count 1 Result
Null or string converts NULL to "" For comparison of numbers or words
Bool or null any other type is converted to bool, FALSE <TRUE
Object built-in classes can define their own comparisons. Different classes cannot be compared. The same classes and arrays are used to compare attributes (in PHP 4). PHP 5 has its own instructions.

String, resource, or number string, resource, or number converts a string and a resource to a number.
Array has a small array of fewer Members. If the key in Operation Number 1 does not exist in Operation Number 2, the array cannot be compared; otherwise, the values are compared one by one (see the following example)

Array any other type of array is always larger
Any other object type of the object is always larger.

Example 1. Standard array comparison code

 
 
  1. <? Php
  2. // Arrays are compared using standard comparison operators.
  3. Function standard_array_compare ($ op1, $ op2)
  4. {
  5. If (count ($ op1)< Count($ Op2 )){
  6. Return-1; // $ op1<$ Op2
  7. } Elseif (count ($ op1)>Count ($ op2 )){
  8. Return 1; // $ op1>$ Op2
  9. }
  10. Foreach ($ op1 as $Key=>$ Val ){
  11. If (! Array_key_exists ($ key, $ op2 )){
  12. Return null; // uncomparable
  13. } Elseif ($ val<$ Op2 [$ key]) {
  14. Return-1;
  15. } Elseif ($ val>$ Op2 [$ key]) {
  16. Return 1;
  17. }
  18. }
  19. Return 0; // $Op1==$ Op2
  20. }
  21. ?> 

Comparison operators in PHP

Another conditional operator is "? : "(Or ternary) operator. Example 2. Assign the default value

 
 
  1. < ?php  
  2. // Example usage for: Ternary Operator  
  3. $action = (empty($_POST['action'])) 
    ? 'default' : $_POST['action'];  
  4. // The above is identical to 
    this if/else statement  
  5. if (empty($_POST['action'])) {  
  6. $action = 'default';  
  7. } else {  
  8. $action = $_POST['action'];  
  9. }  
  10. ?>   

Expression (expr1 )? (Expr2): (expr3) when the value of expr1 is TRUE, the value is expr2, and when the value of expr1 is FALSE, the value is expr3.

Note: The ternary operator is a statement, so its evaluation is not a variable, but the result of the statement. It is important to return a variable through reference. Return $ var = 42? $ A: $ B; will not work, and a warning will be issued for future PHP versions.


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.