PHP Comparison Operators Tutorial with Examples

Comparison operators in PHP are used to compare two values.

These operators return a boolean result (true or false) based on the comparison. PHP provides several comparison operators to handle different types of comparisons, such as equality, inequality, greater than, less than, and more.

In this tutorial, we will cover:

Let’s explore each of these comparison operators with detailed explanations and examples.

1. Equal (==)

The equal operator (==) checks if two values are equal. It does not check the type of the variables; it only checks if the values are the same.

Syntax:

$a == $b

Example:

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

if ($a == $b) {
    echo "The values are equal";
} else {
    echo "The values are not equal";
}
?>

Output:

The values are equal
  • Although $a is an integer and $b is a string, the == operator compares their values and considers them equal.

2. Identical (===)

The identical operator (===) checks if two values are equal and of the same type. This is stricter than the == operator.

Syntax:

$a === $b

Example:

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

if ($a === $b) {
    echo "The values and types are identical";
} else {
    echo "The values or types are not identical";
}
?>

Output:

The values or types are not identical
  • Here, $a is an integer and $b is a string. Although the values are the same, the types are different, so the result is false.

3. Not Equal (!= or <>)

The not equal operator (!= or <>) checks if two values are not equal. Like the == operator, it does not check the type.

Syntax:

$a != $b
$a <> $b

Example:

<?php
$a = 5;
$b = "10";

if ($a != $b) {
    echo "The values are not equal";
} else {
    echo "The values are equal";
}
?>

Output:

The values are not equal
  • Since $a is 5 and $b is “10”, the values are not equal, and the result is true.

4. Not Identical (!==)

The not identical operator (!==) checks if two values are not equal or not of the same type. It returns true if the values or types are different.

Syntax:

$a !== $b

Example:

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

if ($a !== $b) {
    echo "The values or types are not identical";
} else {
    echo "The values and types are identical";
}
?>

Output:

The values or types are not identical
  • Here, although the values are equal, the types are different (integer vs. string), so the result is true.

5. Greater Than (>)

The greater than operator (>) checks if the value on the left is greater than the value on the right.

Syntax:

$a > $b

Example:

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

if ($a > $b) {
    echo "$a is greater than $b";
} else {
    echo "$a is not greater than $b";
}
?>

Output:

10 is greater than 5
  • Since $a is 10 and $b is 5, the result is true.

6. Less Than (<)

The less than operator (<) checks if the value on the left is less than the value on the right.

Syntax:

$a < $b

Example:

<?php
$a = 3;
$b = 7;

if ($a < $b) {
    echo "$a is less than $b";
} else {
    echo "$a is not less than $b";
}
?>

Output:

3 is less than 7
  • Since $a is 3 and $b is 7, the result is true.

7. Greater Than or Equal To (>=)

The greater than or equal to operator (>=) checks if the value on the left is greater than or equal to the value on the right.

Syntax:

$a >= $b

Example:

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

if ($a >= $b) {
    echo "$a is greater than or equal to $b";
} else {
    echo "$a is less than $b";
}
?>

Output:

10 is greater than or equal to 10
  • The values of $a and $b are equal, so the result is true.

8. Less Than or Equal To (<=)

The less than or equal to operator (<=) checks if the value on the left is less than or equal to the value on the right.

Syntax:

$a <= $b

Example:

<?php
$a = 5;
$b = 8;

if ($a <= $b) {
    echo "$a is less than or equal to $b";
} else {
    echo "$a is greater than $b";
}
?>

Output:

5 is less than or equal to 8
  • Since $a is 5 and $b is 8, the result is true.

9. Spaceship Operator (<=>)

The spaceship operator (<=>) is a combined comparison operator introduced in PHP 7. It returns:

  • -1 if the value on the left is less than the value on the right,
  • 0 if the values are equal,
  • 1 if the value on the left is greater than the value on the right.

Syntax:

$a <=> $b

Example:

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

$result = $a <=> $b;

if ($result == -1) {
    echo "$a is less than $b";
} elseif ($result == 0) {
    echo "$a is equal to $b";
} else {
    echo "$a is greater than $b";
}
?>

Output:

5 is less than 10
  • The spaceship operator compares $a and $b and returns -1 because $a is less than $b.

Another Example (When Values are Equal):

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

$result = $a <=> $b;

if ($result == -1) {
    echo "$a is less than $b";
} elseif ($result == 0) {
    echo "$a is equal to $b";
} else {
    echo "$a is greater than $b";
}
?>

Output:

20 is equal to 20

Summary of PHP Comparison Operators:

Operator Description Example Result
== Equal to $a == $b True if equal
=== Identical (equal and same type) $a === $b True if identical
!= or <> Not equal to $a != $b True if not equal
!== Not identical (not equal or not same type) $a !== $b True if not identical
> Greater than $a > $b True if $a is greater
< Less than $a < $b True if $a is less
>= Greater than or equal to $a >= $b True if greater or equal
<= Less than or equal to $a <= $b True if less or equal
<=> Spaceship (combined comparison) $a <=> $b `-1, 0, 1

 

Conclusion

Comparison operators in PHP are crucial for writing conditional statements and controlling the flow of logic.

Understanding how these operators work will allow you to compare values effectively in your programs. In this tutorial, we covered:

  • Equality and identity operators (==, ===)
  • Inequality and non-identity operators (!=, !==)
  • Greater than, less than, and spaceship operators (<=>)

By mastering these comparison operators, you will be able to write more robust and reliable PHP code.

 

Related posts

PHP Static Methods: A Tutorial with Examples

PHP Deleting Files: A Tutorial with Examples

PHP Copying Files: A Tutorial with Examples