PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

Comparison operators in PHP are used to compare two values (numbers or strings). The result of the comparison can be true, false, or sometimes a type-specific comparison result. Understanding these operators is crucial for controlling the flow of a PHP script. Here is an in-depth look at the various comparison operators available in PHP:

1. Equal (==)

  • Usage: $a == $b
  • Description: Returns true if $a is equal to $b after type juggling (i.e., automatic type conversion if necessary).
  • Example:
				
					$a = 5; 
$b = "5";
var_dump($a == $b); // true, because the string "5" is converted to the integer 5

				
			

2. Identical (===)

  • Usage: ‘$a === $b
  • Description: Returns true if ‘$a‘ is equal to ‘$b‘, and they are of the same type (no type juggling).
  • Example:
				
					$a = 5; 
$b = "5";
var_dump($a === $b); // false, because the types are different (integer vs string)

				
			

3. Not Equal (!= or )

  • Usage: ‘$a != $b‘ or ‘$a <> $b
  • Description: Returns true if $a is not equal to $b after type juggling.
  • Example:
				
					$a = 5; 
$b = "6";
var_dump($a != $b); // true, because 5 is not equal to 6
var_dump($a <> $b); // true, same as above

				
			

4. Not Identical (!==)

  • Usage: ‘$a !== $b
  • Description: Returns true if ‘$a‘ is not equal to ‘$b‘, or they are not of the same type.
  • Example
				
					$a = 5; 
$b = "5";
var_dump($a !== $b); // true, because the types are different

				
			

5. Less Than (<)

  • Usage: ‘$a < $b
  • Description: Returns true if ‘$a‘ is strictly less than ‘$b‘.
  • Example:
				
					$a = 5; 
$b = 10;
var_dump($a < $b); // true, because 5 is less than 10

				
			

6. Greater Than (>)

  • Usage: ‘$a > $b
  • Description: Returns true if ‘$a‘ is strictly greater than ‘$b‘.
  • Example:
				
					$a = 10; 
$b = 5;
var_dump($a > $b); // true, because 10 is greater than 5

				
			

7. Less Than or Equal To (<=)

  • Usage: ‘$a <= $b
  • Description: Returns true if ‘$a‘ is less than or equal to ‘$b‘.
  • Example:
				
					$a = 5; 
$b = 5;
var_dump($a <= $b); // true, because 5 is equal to 5

				
			

8. Greater Than or Equal To (>=)

  • Usage: ‘$a >= $b
  • Description: Returns true if ‘$a‘ is greater than or equal to ‘$b‘.
  • Example:
				
					$a = 10; 
$b = 5;
var_dump($a >= $b); // true, because 10 is greater than 5

				
			

9. Spaceship Operator ()

  • Usage: ‘$a <=> $b
  • Description: Returns an integer less than, equal to, or greater than zero if ‘$a‘ is respectively less than, equal to, or greater than ‘$b‘.
  • Example:
				
					$a = 10; 
$b = 5;
var_dump($a >= $b); // true, because 10 is greater than 5

				
			

Type Juggling in PHP

Type juggling in PHP refers to the automatic conversion of one data type to another when required. This is an important concept to understand when dealing with comparison operators because it can lead to unexpected results if not carefully considered. For example:

				
					$a = 0;
$b = "0";
var_dump($a == $b); // true, because "0" is converted to 0
var_dump($a === $b); // false, because the types are different (integer vs string)

				
			

Summary

  • == checks for equality after type juggling.
  • === checks for equality without type juggling.
  • != or <> checks for inequality after type juggling.
  • !== checks for inequality without type juggling.
  • <, >, <=, >= are straightforward arithmetic comparisons.
  • <=> provides a three-way comparison result.

Understanding these operators and how PHP handles type juggling is crucial for writing robust and bug-free code.

Scroll to Top