PHP - Comparison Operators

Comparison operators in PHP are used to compare two values. They help in making decisions within conditional statements like if, while, and switch.

In this guide, we will explore different comparison operators in PHP and see how they work with practical examples.


List of PHP Comparison Operators

PHP provides several comparison operators:

OperatorDescriptionExample ($a = 10, $b = 20)Returns
==Equal to$a == $bfalse
===Identical (Equal & same type)$a === $bfalse
!= or < >Not equal$a != $btrue
!==Not identical$a !== $btrue
>Greater than$a > $bfalse
<Less than$a < $btrue
>=Greater than or equal to$a >= $bfalse
<=Less than or equal to$a <= $btrue
<=>Spaceship operator$a <=> $b-1

Examples of PHP Comparison Operators

1. Equal (==)

The == operator checks if two values are equal, ignoring their data types.

<?php $a = 10; $b = "10"; if ($a == $b) { echo "Equal"; } else { echo "Not Equal"; } // Output: Equal ?>

2. Identical (===)

The === operator checks both value and type.

<?php $a = 10; $b = "10"; if ($a === $b) { echo "Identical"; } else { echo "Not Identical"; } // Output: Not Identical ?>

3. Not Equal (!= or < >)

The != or < > operator checks if values are not equal.

<?php $a = 10; $b = 20; if ($a != $b) { echo "Values are different"; } else { echo "Values are same"; } // Output: Values are different ?>

4. Not Identical (!==)

The !== operator checks if values or types are different.

<?php $a = 10; $b = "10"; if ($a !== $b) { echo "Values or types are different"; } else { echo "Values and types are same"; } // Output: Values or types are different ?>

5. Greater Than (>) and Less Than (<)

The > operator checks if the left value is greater, while < checks if it's smaller.

<?php $a = 30; $b = 20; if ($a > $b) { echo "$a is greater than $b"; } else { echo "$a is not greater than $b"; } // Output: 30 is greater than 20 ?>

6. Greater Than or Equal To (>=) and Less Than or Equal To (<=)

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

7. Spaceship Operator (<=>)

The <=> operator returns:

  • 0 if both values are equal
  • 1 if the left value is greater
  • -1 if the right value is greater
<?php $a = 10; $b = 20; $result = $a <=> $b; echo $result; // Output: -1 (Because 10 is less than 20) ?>

Practical Use Cases

1. Conditional Statements

Comparison operators are commonly used in if-else conditions:

<?php $age = 18; if ($age >= 18) { echo "You are eligible to vote."; } else { echo "You are not eligible to vote."; } ?>

2. Loops

They help in loop conditions like while and for:

<?php $count = 1; while ($count <= 5) { echo "Count: $count\n"; $count++; } ?>

Key Takeaways

  1. Use == for value comparison, but prefer === for strict type checking.
  2. The != and < > operators work the same way, but !== checks type as well.
  3. The spaceship operator (<=>) is useful for sorting functions.
  4. Always validate data types when comparing values, especially user input.

Conclusion

PHP comparison operators are essential for making logical decisions in your applications. They help in evaluating conditions, looping through data, and comparing values effectively.

By mastering these operators, you can write more reliable and efficient PHP programs. Start practicing with real-world scenarios and improve your PHP coding skills!