PHP Comparison Operators

In this chapter you will learn:

  1. What are Comparison Operators
  2. What are PHP Comparison Operators

Description

Comparison operators return either true or false, and thus are suitable for use in conditions.

PHP Comparison Operators

Comparison operators in PHP has are listed in the following table.

Operator Name Description
==Equals True if $a is equal to $b
=== Identical True if $a is equal to $b and of the same type
!=Not equal True if $a is not equal to $b
<>Not equal True if $a is not equal to $b
!= = Not identical True if $a is not equal to $b or if they are not of the same type
< Less than True if $a is less than $b
> Greater than True if $a is greater than $b
<=Less than or equal True if $a is less than or equal to $b
>=Greater than or equal True if $a is greater than or equal to $b

The === (identical) says two variables are only identical if they hold the same value and if they are the same type, as demonstrated in this code example:


<?PHP//from ja  v a  2s  .c om
     print 12 == 12; 
     print 12.0 == 12; 
     print (0 + 12.0) == 12; 
     print 12  === 12; 
     print "12" == 12; 
     print "12" === 12; 
?>

The === operator is useful when comparing against false with 0 and empty string.

For example, PHP considers an empty string (""), 0, and false to be equal when used with ==, but using === allows you to make the distinction. For example:


<?PHP/*from   j  av a2 s  . c  o m*/
     if (0 === true) {                                                                               
        print(" this is true ");
     }                                                                                                
     if (0 === false) { 
        print("this is false ");
     } 
?>

Next chapter...

What you will learn in the next chapter:

  1. What are Logical Operators
  2. What are PHP Logical Operators
  3. What values are considered false
Home » PHP Tutorial » PHP Operators
PHP Assignment Operators
PHP Arithmetic Operators
PHP Incrementing and Decrementing Operators
PHP Comparison Operators
PHP Logical Operators
PHP String Operators
PHP Ternary Operator
PHP Bitwise Operators
PHP Execution Operator
PHP Operator Precedence