Wednesday, July 3, 2024
HomeLanguagesPhpDifference between $a != $b and $a !== $b

Difference between $a != $b and $a !== $b

$a!=$b

This operator is also known as inequality operator. It is used to check the equality of both operands, however, the type of the operands does not match. For instance, an integer type variable is equivalent to the floating point number if the value of both the operands is same. It is a binary operator. 

PHP




<?php
 
// Assigning values to the
// variables a and b
$a = 5;
$b = 5.0;
 
if($a != $b) {
    echo("Both operands are not Equal");
}
else {
    echo("Both operands are Equal");
}
 
?>


Output

 Both operands are Equal

$a!==$b

This is non-identity operator. It is used to check the equality of both operands, however, the type of the operands also match. For instance, an integer type variable is not equivalent to the floating point number irrespective of the value of both the operands. It is a binary operator. 

PHP




<?php
 
// Assigning the values of
// variables a and b
$a = 5;
$b = 5.0;
 
if($a !== $b) {
    echo("Both operands are not same");
}
else {
    echo("Both operands are same");
}
 
?>


Output

Both operands are not same

Difference between != and !== operators:

!=

!==

It returns true if value of both operands are equal. It returns true if value and data type of both operands are equal.
It is known as Inequality Operator. It is known as Non-identity Operator.
Empty values are considered to be equivalent. Empty values should belong to same data type.

Example: The following code snippet illustrates the difference of both the operators in a simple way. 

PHP




<?php
   
// Assigning values to the
// variables a and b
$a = null;
$b = FALSE;
 
// != operator
if($a != $b) {
    echo("Both operands are not equal (Only Value)\n");
}
else {
    echo("Both operands are equal (Only Value)\n");
}
 
// !== operator
if($a !== $b) {
    echo("Both operands are not equal (Value and Data Types)");
}
else {
    echo("Both operands are equal (Value and Data Types)");
}
 
?>


Output

Both operands are equal (Only Value)
Both operands are not equal (Value and Data Types)

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments