Learn PHP


PHP Comparison operator


Comparison operators, as their name implies, allow you to compare two values. You may also be interested in viewing the type comparison tables, as they show examples of various type related comparisons.


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


The PHP comparison operators are used to compare two values (number or string):


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


Operator Name Example Description
== Equals $a == $b TRUE if $a is equal to $b after type juggling.
=== Identical $a === $b TRUE if $a is equal to $b, and they are of the same type.
!= Not equal $a != $b TRUE if $a is not equal to $b after type juggling.
<> Not equal $a <> $b Not equal TRUE if $a is not equal to $b after type juggling.
!== Not identical $a !== $b TRUE if $a is not equal to $b, or they are not of the same type.
< Less than $a < $b TRUE if $a is strictly less than $b.
> Greater than $a > $b TRUE if $a is strictly greater than $b.
<= Less than or equal $a <= $b TRUE if $a is less than or equal to $b.
>= Greater than or equal $a >= $b TRUE if $a is greater than or equal to $b.
<=> Spaceship $a <=> $b An integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Available as of PHP 7.

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.


Example Identical Operator


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
				     if (0 === true) {                                                                               
				        print(" this is true ");
				     }                                                                                                
				     if (0 === false) { 
				        print("this is false ");
				     } 
				?>
		  

Example


		  		<?php
		  			var_dump(0 == "a"); // 0 == 0 -> true
					var_dump("1" == "01"); // 1 == 1 -> true
					var_dump("10" == "1e1"); // 10 == 10 -> true
					var_dump(100 == "1e2"); // 100 == 100 -> true

					switch ("a") {
					case 0:
					    echo "0";
					    break;
					case "a": // never reached because "a" is already matched with 0
					    echo "a";
					    break;
					}
		  		?>
		  

Example


		  		<?php
		  			// Integers
					echo 1 <=> 1; // 0
					echo 1 <=> 2; // -1
					echo 2 <=> 1; // 1
					 
					// Floats
					echo 1.5 <=> 1.5; // 0
					echo 1.5 <=> 2.5; // -1
					echo 2.5 <=> 1.5; // 1
					 
					// Strings
					echo "a" <=> "a"; // 0
					echo "a" <=> "b"; // -1
					echo "b" <=> "a"; // 1
					 
					echo "a" <=> "aa"; // -1
					echo "zz" <=> "aa"; // 1
					 
					// Arrays
					echo [] <=> []; // 0
					echo [1, 2, 3] <=> [1, 2, 3]; // 0
					echo [1, 2, 3] <=> []; // 1
					echo [1, 2, 3] <=> [1, 2, 1]; // 1
					echo [1, 2, 3] <=> [1, 2, 4]; // -1
					 
					// Objects
					$a = (object) ["a" => "b"]; 
					$b = (object) ["a" => "b"]; 
					echo $a <=> $b; // 0
					 
					$a = (object) ["a" => "b"]; 
					$b = (object) ["a" => "c"]; 
					echo $a <=> $b; // -1
					 
					$a = (object) ["a" => "c"]; 
					$b = (object) ["a" => "b"]; 
					echo $a <=> $b; // 1
					 
					// not only values are compared; keys must match
					$a = (object) ["a" => "b"]; 
					$b = (object) ["b" => "b"]; 
					echo $a <=> $b; // 1
		  		?>
		  

Example


		  		<?php
		  			$a = 100;  
					$b = "100";

					var_dump($a == $b); // returns true because values are equal
		  		?>
		  


Example


		  		<?php
		  			$a = 100;  
					$b = "100";

					var_dump($a === $b); // returns false because types are not equal
		  		?>
		  

Example


		  		<?php
		  			$a = 100;  
					$b = "100";

					var_dump($a != $b); // returns false because values are equal
		  		?>
		  

Example


		  		<?php
		  			$a = 100;  
					$b = "100";

					var_dump($a <> $b); // returns false because values are equal
		  		?>
		  

Example


		  		<?php
		  			$a = 100;  
					$b = "100";

					var_dump($a !== $b); // returns true because types are not equal
		  		?>
		  

Example


		  		<?php
		  			$a = 100;
					$b = 50;

					var_dump($a > $b); // returns true because $a is greater than $b
		  		?>
		  

Example


		  		<?php
		  			$a = 10;
					$b = 50;

					var_dump($a < $b); // returns true because $a is less than $b
		  		?>
		  


Example


		  		<?php
		  			$a = 50;
					$b = 50;

					var_dump($a >= $b); // returns true because $a is greater than or equal to $b
		  		?>
		  

Example


		  		<?php
		  			$a = 50;
					$b = 50;

					var_dump($a <= $b); // returns true because $a is less than or equal to $b
		  		?>
		  

Example Boolean/null comparison


		  		<?php
		  			// Bool and null are compared as bool always
					var_dump(1 == TRUE);  // TRUE - same as (bool)1 == TRUE
					var_dump(0 == FALSE); // TRUE - same as (bool)0 == FALSE
					var_dump(100 < TRUE); // FALSE - same as (bool)100 < TRUE
					var_dump(-10 < FALSE);// FALSE - same as (bool)-10 < FALSE
					var_dump(min(-100, -10, NULL, 10, 100)); // NULL - (bool)NULL < (bool)-100 is FALSE < TRUE
		  		?>
		  

Example array comparison


		  		<?php
		  			// Arrays are compared like this with standard comparison operators
					function standard_array_compare($op1, $op2)
					{
					    if (count($op1) < count($op2)) {
					        return -1; // $op1 < $op2
					    } elseif (count($op1) > count($op2)) {
					        return 1; // $op1 > $op2
					    }
					    foreach ($op1 as $key => $val) {
					        if (!array_key_exists($key, $op2)) {
					            return null; // uncomparable
					        } elseif ($val < $op2[$key]) {
					            return -1;
					        } elseif ($val > $op2[$key]) {
					            return 1;
					        }
					    }
					    return 0; // $op1 == $op2
					}
		  		?>
		  




PHP Tutorials


Home

Introduction

Syntax

Variable

Comment

Echo and Print Statement

Data Types

Boolean

Integer

Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion

file i/o