Open In App

Difference between the (=), (==), and (===) operators in PHP

Last Updated : 28 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In PHP, the ‘=’ operator is used for assignment, while the ‘==’ operator is used for loose equality comparison, meaning it checks if two values are equal without considering their data types. On the other hand, the ‘===’ operator is used for strict equality comparison, meaning it checks if two values are equal and of the same data type.

= Operator

The equals to ‘=’ sign operator is used as an assignment operator. It assigns the value that is on its right side to the variable that is on its left side.

Syntax:

$variable = value

Example: Implementation to showcase the use of ‘=’ operator.

PHP
<?php
$num1 = 7;
$num2 = 8.99;
$num3 = $num1 + $num2;
echo $num3 . "\n";

$name = "GeeksforGeeks";
echo "You are on portal of " . $name . "\n";

$single = true;
echo $single . "\n";
?>

Output
15.99
You are on portal of GeeksforGeeks
1

== Operator

The ‘==’ is not used for assignment purpose. It is only used to compare the values of two given operands, not their data types. This operator is used to check if the given values are equal or not. If both the values are equal, it returns true, otherwise it returns false.

In below example, the string “34” is automatically converted to the integer 34 for the comparison, and since both values are numerically equal, the condition is true and “Equal” is echoed.

Syntax:

operand1 == operand2

Example: Implementation to showcase the use of ‘==’ operator.

PHP
<?php
$a = 34;
$b = 34;
// When both have equal 
// value and data type
if ($a == $b) {
    echo "Equal";
} else {
    echo "Not Equal";
}

echo "\n";
// When both have equal value
// but different data type
if ("34" == 34) {
    echo "Equal";
} else {
    echo "Not Equal";
}
?>

Output
Equal
Equal

=== Operator

The ‘===’ won’t do assignment either. It is used to compare both values and data types of two given operands. It is a more strict version of ‘==’ operator. This operator is used to check if the given values and their data types are equal or not. If both the values are equal, it returns true, otherwise it returns false.

Syntax:

operand1 === operand2

Example: Implementation to showcase the use of ‘===’ operator.

PHP
<?php
$a = 34;
$b = 34;
// When both have equal
// value and data type
if ($a == $b) {
    echo "Equal";
} else {
    echo "Not Equal";
}

echo "\n";
// When both have equal value
// but different data type
if ("34" === 34) {
    echo "Equal";
} else {
    echo "Not Equal";
}
?>

Output
Equal
Not Equal

Difference between the (=), (==), and (===) operators

Aspect= (Assignment)== (Equality)=== (Identity)
Type ConversionNo type conversion.Performs type conversion before comparison.Does not perform type conversion; checks both the values and the types for equality.
Return ValueNo return value.Returns true if the values are equal, false otherwise.Returns true if the values and types are equal, false otherwise.
UsageUsed for assignment.Used for loose comparison.Used for strict comparison.
Examples$a = 5;$a == $b;$a === $b;
Common MistakesMistakenly used in place of == or ===.Mistakenly used in place of ===.Sometimes overlooked for loose comparisons.
PerformanceNo performance impact.Slightly slower due to type conversion.Slightly faster due to no type conversion.
Use CasesAssigning values to variables.Comparing values where type conversion is acceptable.Comparing values where strict type checking is required.
Type SafetyNot type-safe.Not type-safe.Type-safe.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads