PHP If Single or Double Equals

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






















ExampleNameResult
$a == $bEqualTRUE if $a is equal to $b after type juggling.
$a === $bIdenticalTRUE 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:

  1. Set the value of $a to abc
  2. Rest of the expression is: if($a)
  3. $a is != 0, so it is true MOST IMPORTANT
  4. 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



Leave a reply



Submit