How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
Difference between ==
and ===
The difference between the loosely ==
equal operator and the strict ===
identical operator is exactly explained in the manual:
Comparison Operators
Example | Name | Result |
---|---|---|
$a == $b | Equal | TRUE if $a is equal to $b after type juggling. |
$a === $b | Identical | TRUE if $a is equal to $b, and they are of the same type. |
A php if statement with one equal sign...? What does this mean?
It's a form of shorthand, which is exactly equivalent to this:
$confirmation = $payment_modules->confirmation();
if ($confirmation) {
}
The 3 different equals
You have =
the assignment operator, ==
the 'equal' comparison operator and ===
the 'identical' comparison operator.
$a = $b Assign Sets $a to be equal to $b.
$a == $b Equal TRUE if $a is equal to $b.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4)
For more info on the need for ==
and ===
, and situations to use each, look at the docs.
Three equals signs in php comparison
The easiest way to see it, is that ==
checks equality and ===
checks identicality. Equality will check the value, but identicality will check the variable type too.
Examples:
var_dump('true' == true); // bool(true)
var_dump('true' === true); // bool(false)
Using one equals sign in an if statement
PHP, like also C, allows to write any expression into an if-condition.
This can be an expression like:
if(!$db=new database()) die("NO DATABASE CONNECTION");
But it has rather the background that the expression $a="abc"
is evaluated a different way:
- Set the value of $a to abc
- Rest of the expression is:
if($a)
- $a is != 0, so it is true MOST IMPORTANT
- Expression is true, go into if-case
So the expression is evaluated to if(true) because the priority table of
operators make the expression succeed. It is not a mistake of PHP, it is
rather the definition of TRUE/FALSE and how things get evaluated.
This can also be used in loops like shown in another answer for the loop expression with $db->fetch()
Wordpress foreach if statement shows all users where only one is expected?
My guess is that you're using a single equals (=
) instead of a double equals (==
) to check the person's instrument. A single equals (=
) signifies an assignment, and it returns true if the assignment worked. I think you want to use a double equals (==
) to instead check if $user->instrument
is the same as Tenorsaxofon
.
If this is the case, you'll want to change this:
if ( $user->instrument = 'Tenorsaxofon' )
To this:
if ( $user->instrument == 'Tenorsaxofon' )
(More about that here).
It also could be that $subscribers
contains two users who have their instrument set to Tenorsaxofon
. If there's two subscribers with the same instrument, then your echo
will print Tenorsaxofon
twice, resulting in TenorsaxofonTenorsaxofon
Related Topics
Header("Location") Causes [500] Internal Server Error
Transpose Multidimensional Array and Join Values with Commas
Creating a Soap Call Using PHP with an Xml Body
Whats the Best Way to Do User Authentication in PHP
Setting Environment Variables in Mamp
Generating a Waveform Using Ffmpeg
How to Make My PHP Ide Understand Dependency Injection Containers
How to Discover Rss Feeds for a Given Url
Set Application_Env via Virtual Host Config and Read This in PHP
Check If String Is Just White Space
Laravel: Auth::User()->Id Trying to Get a Property of a Non-Object
Is This the Correct Way to Send Email with PHP
PHP Checkbox Set to Check Based on Database Value
Phpexcel_Writer_Exception with Message "Could Not Close Zip File PHP://Output."