Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using one equals sign in an if statement

Tags:

php

In a PHP if statement, when I am comparing a variable I would need to use two == signs. For example,

if($foo == 'bar'){
    //Run code
}

But if I were to use one equals sign like this:

if($foo = 'bar'){
    //Run code
}

This would actually set the $foo variable.

Why would you ever need to set a variable like this inside an if statement? I can't think of one instance where this would be useful, and many programmers I've spoken to feel the same way.

Is there a reason why PHP allows you to set a variable this way?

like image 931
Dan Johnson Avatar asked May 15 '14 08:05

Dan Johnson


People also ask

What does the == sign do in an if statement?

== is an equality comparison operator, it is used to compare two values. So in your if statement you are asking JavaScript to see if divList. style. display is equal to "None" .

What does == === mean?

The === operator means "is exactly equal to," matching by both value and data type. The == operator means "is equal to," matching by value only.

What does a single equal sign mean?

You will also see a single equal sign being used to denote variables and the like. Just keep it all straight by remembering that only the double equal sign means “is equal to” and the single equal sign can be roughly translated into “is.”

What is single equal in JavaScript?

Single = is an assignment operator and will always equate to true in an if statement (assuming it is a non negative value). Double = ,as in == , is a comparison and will equate to true only if the values on either side of the operator are equal. Follow this answer to receive notifications.


3 Answers

Testing the result of a function that returns an error:

if (($err = some_function())) {
    echo "Error: " . $err;
}
like image 96
Jonathon Reinhart Avatar answered Oct 25 '22 23:10

Jonathon Reinhart


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()

like image 43
D. Schalla Avatar answered Oct 25 '22 22:10

D. Schalla


One of the most common usecases is to make sure something exists (or is truthy), and then use it inside the block, for example:

while ($row = $dbConnection->fetch()) { //Make sure that $dbConnection->fetch() is not false or NULL...
    //You can use $row here!
}
like image 41
Madara's Ghost Avatar answered Oct 25 '22 23:10

Madara's Ghost