Difference between the (=), (==), and (===) operators in PHP
Last Updated :
28 Mar, 2024
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";
?>
Output15.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";
}
?>
=== 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";
}
?>
Difference between the (=), (==), and (===) operators
Aspect | = (Assignment) | == (Equality) | === (Identity) |
---|
Type Conversion | No type conversion. | Performs type conversion before comparison. | Does not perform type conversion; checks both the values and the types for equality. |
Return Value | No return value. | Returns true if the values are equal, false otherwise. | Returns true if the values and types are equal, false otherwise. |
Usage | Used for assignment. | Used for loose comparison. | Used for strict comparison. |
Examples | $a = 5; | $a == $b; | $a === $b; |
Common Mistakes | Mistakenly used in place of == or === . | Mistakenly used in place of === . | Sometimes overlooked for loose comparisons. |
Performance | No performance impact. | Slightly slower due to type conversion. | Slightly faster due to no type conversion. |
Use Cases | Assigning values to variables. | Comparing values where type conversion is acceptable. | Comparing values where strict type checking is required. |
Type Safety | Not type-safe. | Not type-safe. | Type-safe. |
Similar Reads
What is the difference between == and === in PHP ? 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: oper
2 min read
What's the difference between
and
in PHP ? There are many types of line-ending characters that are used in PHP. They differ depending upon the operating system and editors using them. It's also based on how apps, libraries, protocols, and file formats deal with things. These characters are invisible. \n is used for the newline or linefeed, w
2 min read
Difference between $var and $$var in PHP In PHP, $var is used to store the value of the variable like Integer, String, boolean, character. $var is a variable and $$var stores the value of the variable inside it. $var: Syntax: $variable = value;The $variable is the variable nameThe value is the initial value of the variable. Example 1: This
2 min read
Difference between die() and exit() functions in PHP PHP exit() Function: In PHP, the exit() function prints a message and exits the application. It's often used to print a different message in the event of a mistake. Use exit() when there is not an error and have to stop the execution. Syntax: exit("Message goes here"); or exit(); Example: exit("This
2 min read
What is the difference between var_dump() and print_r() in PHP ? In this article, we will discuss the difference between var_dump() and print_r() function in PHP. var_dump() Function: The var_dump() function is used to dump information about a variable that displays structured information such as the type and value of the given variable. Syntax: void var_dump ($e
3 min read