What is the !== or === (i guess) operator all about?
| SitePoint Sponsor |



What is the !== or === (i guess) operator all about?





Hi,
It's used to check whether or not two values are identical (if both the value and the datatype is the same).
PHP Code:if( 0 == false ) echo 'this will be printed';
if( 0 === false ) echo 'this will not...';
ERIK RIKLUND :: Yes, I've been gone quite a while.
=== is identity
!== is nonidentity
compares the value of the variable(s) and type of the value of the variable(s) in an expression
identity returns true on match and false on mismatch
non-identity returns true on mismatch and false on match
regards,
melan'



thanks


Is there a difference between != and !==? = and == does, but I don't see how != can mean "if it doesn't assign" logic.
There's a huge difference between != and !==. You have to remember that != and == are in the same field, and !== and === are in the same field. The = and == comparison is a different story all together. = is an assignment operator, != and !== are both comparison operators just like == and ===.Originally Posted by Atealtha
PHP Code:<?php
$a = 5; // Assignment operator.
if ($a == 5)
{
echo 'We WILL get here.';
}
if ($a === 5)
{
echo 'We will also get here';
}
if ($a === '5')
{
echo 'We will NOT get here.';
}
if ($a !== 3)
{
echo 'WILL get here';
}
if ($a !== '5')
{
echo 'Will get here.';
}
if ($a != '5')
{
echo 'Will NOT get here.';
}
if ($a = 3)
{
echo 'This will always return true, as this was an ASSIGNMENT operator. This if statemen just overwrote $a to be 3 instead of 5, and the assignment ends up returning true.';
}![]()
- Nathan
Hi there,
just to give a flat definition for the operators:
=== called the identity operator
checks both value AND dataType of value when doing a comparison returns true on match
!== called the nonIdentity operator
checks both value AND dataType of value when doing a comparison returns true on mismatch
== is called the Equality operator
checks values (only) when doing a comparison returns true on match
!= is called the Inequality operator
checks values (only) when doing a comparison returns true on mismatch
<> (also) called inequality operator
checks values (only) when doing a comparison returns true on mismatch
regards,
'cholic



It compares variables. It is the same as != and ==, except it also checks if they are the same type of variable.
<(^.^<) \(^.^\) (^.^) (/^.^)/ (>^.^)>
Core 2 Duo E8400 clocked @ 3.375GHz, 2x2GB 800MHz DDR2 RAM
5x SATA drives totalling 2.5TB, 7900GS KO, 6600GT
try this piece of code!!!
PHP Code:<?php
switch ($a = 5)// Assignment operator.
{
case 1;
$a == 5;
echo 'We WILL get here.';
break;
case 2;
$a === 5;
echo 'We will also get here';
break;
case 3;
$a === '5';
echo 'We will NOT get here.';
break;
//repeated steps until the end of your code
}
echo 'This will always return true, as this was an ASSIGNMENT operator. This if statemen just overwrote $a to be 3 instead of 5, and the assignment ends up returning true.';
?>
Bookmarks