PHP comparison operators are used to compare two values. They always return a boolean result (true or false) except the spaceship operator, which returns numeric comparison results.
== compares values only=== compares both value and data type!= or <> checks inequality!== checks inequality with type<=> returns -1, 0, or 1
// Comparing values and data types in PHP
<?php
$a = 10;
$b = "10";
$c = 20;
var_dump($a == $b);
var_dump($a === $b);
var_dump($a != $c);
var_dump($a < $c);
var_dump($a >= 10);
var_dump($a <=> $c);
?>
true for equal values, false when type differs, and -1 when the left value is smaller using the spaceship operator.
// JavaScript-style logic explaining PHP comparison flow
let x = 5;
let y = "5";
console.log(x == y);
console.log(x === y);
Test how PHP handles different types!
=== for safe comparisonsvar_dump() for debuggingcompare.php file== and ===