What is the difference between == and === in PHP ? - GeeksforGeeks (2024)

Table of Contents
PHP PHP Please Login to comment...

Improve

Improve

Like Article

Like

Save

Report

In this article, we will discuss the differences between ‘==’ and ‘===’ operators in PHP. Both are comparison operators used to compare two or more values.

== Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false.

Syntax:

operand1 == operand2

=== Operator: This operator is used to check the given values and its data type are equal or not. If yes, then it returns true, otherwise it returns false.

Syntax:

operand1 === operand2

Note: === operator will return false when data types of operand are different.

Example 1: The following code demonstrates == operator with same and different data type operands.

PHP

<?php

$a = 34;

$b = 34;

// Show message if two operands are

// equal with same data type operands

if($a == $b) {

echo "Equal";

}

else{

echo "Not Equal";

}

echo "\n";

// Show a message if two operands are equal

// with different data type operands

// First is of string type and the second

// is of integer type

if('34' == 34){

echo "Equal";

}

else{

echo "Not Equal";

}

?>

Output:

EqualEqual

Example 2: The following code demonstrates the === operator.

PHP

<?php

$a = 34;

$b = 34;

// Return a message if two operands are

// equal with same data type operands

if($a === $b){

echo "Equal";

}

else{

echo "not Equal";

}

echo "\n";

// Return a message if two operands are equal

// with different data type operands

// First is of string type and the second

// is if integer type

if('34' === 34){

echo "Equal";

}

else{

echo "not Equal";

}

?>

Output:

Equalnot Equal

Difference between == and === operators:

== ===
It is equal to operator. It is an identical operator.
It is used to check the equality of two operands.It is used to check the equality of both operands and their data type.


Last Updated : 18 Oct, 2021

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

What is the difference between == and === in PHP ? - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 5973

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.