PHP comparison operator

Source: Internet
Author: User
Comparison Operators
Example Name Result
$ A = $ B Equal TrueIf$Is equal$ B.
$ A ===$ B Identical TrueIf$Is equal$ B, And they are of the same type. (introduced in PHP 4)
$! = $ B Not equal TrueIf$Is not equal$ B.
$ A <> $ B Not equal TrueIf$Is not equal$ B.
$! ==$ B Not identical TrueIf$Is not equal$ B, Or they are not of the same type. (introduced in PHP 4)
$ A <$ B Less TrueIf$Is strictly less$ B.
$ A> $ B Greater TrueIf$Is strictly greater$ B.
$ A <= $ B Less than or equal TrueIf$Is less than or equal$ B.
$ A> = $ B Greater than or equal TrueIf$Is greater than or equal$ B.

 

Note: If you compare a string with an integer value. String is converted to the number type (integer or float type), and then compared with integer.

 

  1   <?  PHP
2   Var_dump ( 0 = " A " ); // 0 = 0-> true
3   Var_dump ( " 1 " = " 01 " ); // 1 = 1-> true
4 Var_dump ( " 1 " = " 1e0 " ); // 1 = 1-> true
5
6 Switch ( " A " ){
7 Case 0 :
8 Echo " 0 " ;
9 Break ;
10 Case " A " : // Never reached because "A" is already matched with 0
11 Echo " A " ;
12 Break ;
13 }
14 ?>
15

 

 

COMPARISON WITH VARIOUS TYPES
Type of operand 1 Type of operand 2 Result
Null or string String ConvertNullTo "", numerical or lexical comparison
Bool or null Anything Convert to bool,False<True
Object Object Built-in classes can define its own comparison, different classes are uncomparable, same class-compare properties the same way as arrays (PHP 4), PHP 5 has its own explanation
String, resource or number String, resource or number Translate strings and resources to numbers, usual math
Array Array Array with fewer members is smaller, if key from operand 1 is not found in operand 2 then arrays are uncomparable, otherwise-compare value by value (see following example)
Array Anything Array is always greater
Object Anything Object is always greater

 

When comparing two numbers, we 'd better understand all the meanings in this table first. Yes.

I once suffered losses here.

 

In addition, when comparing two values, if you can use "=" and "! The = "operator uses the two operators. This prevents PHP from automatically converting data types.

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.