What is the difference between == and === Operators in PHP?

== (equal) compares only the values, and PHP may change the data type before comparing. === (identical) compares the value and the data type, so it is strict.

<?php
$num_1 = 12;      // integer
$num_2 = "12";    // string

var_dump($num_1 == $num_2);
var_dump($num_1 === $num_2);
var_dump(0 == "abc");
var_dump(null == false);
var_dump(null === false);
?>

Output:

Output in PHP 8

bool(true)
bool(false)
bool(false)
bool(true)
bool(false)

  • The first comparison is true because the values are same, but the second is false because one is an integer and the other is a string.
  • 0 == "abc" was true in PHP 7, but PHP 8 changed it to false.
  • null == false is true, because both are false-like. With === it is false.

Use === in your conditions. The opposite operators are != and !==, see != vs !==. All operators are in PHP Operators.


Learn more about the similar topics:
Tutorials
PHP Operators
PHP Ternary Operator
Exercises & Assignments
Write a PHP program to add two numbers
Interview Questions & Answers
What is the difference between == and === Operators in PHP?
What are the arithmetic operators in PHP?
What is the use of the ternary operator in PHP?
What is the null coalescing operator (??) in PHP?
What is the difference between division (/) and modulus (%) in PHP?
What is the difference between != and !== in PHP?
What is the difference between $var++ and $var + 1 in PHP?