This question already has answers here:
Does variables' order matter when doing == and === comparisons? [duplicate]
(3 answers)
Closed 3 years ago.
Original question
i read sometime ago in an article i can't find that when doing a comparison the order matters for some reason.
i couldn't find any information on the subject of the order of comparing in PHP.
it obviously won't make a dramatic change but i am curious to know if there is any merit to this.
throughout the project i am working on the comparison is done as $x === true.
is there any difference in doing the comparison in the opposite order, as in true === $x?
Conclusion
Apparently what i referred to in the original question is a programming style called "Yoda conditions".
This wiki page gives a good explanation about this style.
This answer made me understand the concept, just note there is a small mistake there.
Here's my take on it:
The main reason to use this style is to avoid an accidental assignment with = when you meant to compare with ==.
if you want to check if a variable loosely has the same value as what you compare it to, use:
if(false == $var) // evaluates to true if $var is equal to false
over
if($var == false) // evaluates to true if false is equal to $var
to prevent
if($var = false) // assigns false to $var and evaluates to false
while
if(false = $var) // is a syntax error
if you want to check if a variable strictly has the same value as what you compare it to, use:
if(false === $var) // evaluates to true if $var is identical to false
over
if($var === false) // evaluates to true if false is identical to $var
it doesn't matter in the context of using Yoda style or not as this isn't really an issue with strict comparison because it's pretty hard to confuse = with === but if you use it for == then also use it for === to be consistent.
All in all, I think the merit of Yoda notation lies in the emphasized distinction it makes between comparison and assignment in a condition.
Those are called "Yoda Comparisons". For === and == it would not matter, but consider this:
if($a = 0)
In this case $a is being set to zero, not compared. In addition, the comparison you think you're making will not evaluate at all, but the comparison operation will proceed as if there was a comparison made. It will always fall through to false Flip it:
if(0 = $a)
Now an error would be thrown.
Related
This question already has answers here:
Does variables' order matter when doing == and === comparisons? [duplicate]
(3 answers)
Closed 3 years ago.
Original question
i read sometime ago in an article i can't find that when doing a comparison the order matters for some reason.
i couldn't find any information on the subject of the order of comparing in PHP.
it obviously won't make a dramatic change but i am curious to know if there is any merit to this.
throughout the project i am working on the comparison is done as $x === true.
is there any difference in doing the comparison in the opposite order, as in true === $x?
Conclusion
Apparently what i referred to in the original question is a programming style called "Yoda conditions".
This wiki page gives a good explanation about this style.
This answer made me understand the concept, just note there is a small mistake there.
Here's my take on it:
The main reason to use this style is to avoid an accidental assignment with = when you meant to compare with ==.
if you want to check if a variable loosely has the same value as what you compare it to, use:
if(false == $var) // evaluates to true if $var is equal to false
over
if($var == false) // evaluates to true if false is equal to $var
to prevent
if($var = false) // assigns false to $var and evaluates to false
while
if(false = $var) // is a syntax error
if you want to check if a variable strictly has the same value as what you compare it to, use:
if(false === $var) // evaluates to true if $var is identical to false
over
if($var === false) // evaluates to true if false is identical to $var
it doesn't matter in the context of using Yoda style or not as this isn't really an issue with strict comparison because it's pretty hard to confuse = with === but if you use it for == then also use it for === to be consistent.
All in all, I think the merit of Yoda notation lies in the emphasized distinction it makes between comparison and assignment in a condition.
Those are called "Yoda Comparisons". For === and == it would not matter, but consider this:
if($a = 0)
In this case $a is being set to zero, not compared. In addition, the comparison you think you're making will not evaluate at all, but the comparison operation will proceed as if there was a comparison made. It will always fall through to false Flip it:
if(0 = $a)
Now an error would be thrown.
This question already has answers here:
Variable position in comparision in PHP
(4 answers)
Closed 2 years ago.
I've seen a lot of codes that places boolean in front, followed by comparison operator, and then the variable/function to test
if (true === something()) {
doStuff();
}
Instead of the usual
if (something() === true) {
doStuff();
}
Is there a real difference between them instead of personal preference?
The practice to put the value first for comparisons originates from a time when == was commonly used as a comparison operator (instead of === as today) and syntax highlighting and linting was not a standard. We just used simple text editors to write PHP at that time.
The "yoda style" syntax helped to avoid fatal typos in comparisons involving a variable:
$foo = false;
if ($foo = true) ...
silently results in true being assigned to variable $foo, which might not be the intention. (Also, the if is always true)
Whereas
$foo = false;
if (true = $foo) ...
results in a syntax error, saving you from a headache.
This question already has answers here:
What's the meaning of the reverse entry (null === $value) when checking the value of the variable? [duplicate]
(4 answers)
Closed 6 years ago.
Often, I see in if statements for php something like this:
if (null === $variable) {
// do stuff
}
What does it do and what is it for?
EDIT: I totally get that it is a comparison operator, I just wonder why not $variable === null.
It's not an assignment, it's a comparison for equality. It determines if the variable $variable contains the value null.
More in the documentation:
Assignment Operators
Comparison Operators
why not to check $variable === null
Some people like to use the form with the constant on the left (a "Yoda condition", it is called) so that if they have a typo and only type a single =, it causes a syntax error rather than doing an assignment.
That is the Yoda style usually used as a trick by programmers to prevent accidental assignments which always give some silent bugs.
Example:
var a = dosomething();
if(a = null){
//more here
}
Note that the if block will always not execute regardless of the result of doSomething method since we assign then check for equality. This assignment nullifies the possibly non-deterministic nature of doSomething
This question already has answers here:
'AND' vs '&&' as operator
(10 answers)
Closed 7 years ago.
Why is $x true in this statment?
$x = true and false;
I've got the problem with some variables but I could reduce the problem to the primitive boolean values.
Update:
As you see in the replies the effect has to do with the operator precedense in PHP. There is also a good explanation about the problem in this question, which I couldn't find in the net before since I didn't know that I have a problem with this and I didn't know that there is a difference between '&&'/'||' and 'and'/'or'.
After some search I found out that this is caused by the operator precedence in PHP. The '=' operator is stronger than 'and'.
If you want to get the expected result you have to use braces:
$x = (true and false); //correct: $x is false now!
Without braces the expression is equal to ($x = true) and false; . $x will get the 'true' value. After that the PHP interpreter 'replaces' this assignment with the value that $x has just got. So true and false; remains. And that does not do anything. 'false' is lost and didn't influence the expression.
Be aware that the braces are NOT required if you use '&&' or '||'! They are stronger than '=' and thus stronger than 'and' and 'or'. Strange...
This is different to e.g. $x = 5 + 6; since here '+' is stronger than '=' it will be resolved first. In the end it affects only the boolean operators 'and', 'or' and 'xor'. Only with them you have to watch out.
Note also that this is different compared to other languages like JavaScript or Java. In those languages you don't need the braces since the logical operators are stronger than the equal operator (Operators in JavaScript, Operators in Java).
More on this:
$x = true and false;
So we have the following. If you guessed that $x is true, you'd be right. Since = has a higher precedent than and.
Rewriting the expression above it would be equal to this:
($x = true) and false;
Here's where it gets funny.
If you were to do:
$x = true && false;
and you'd guess that $x is true, you'd be wrong. $x is actually false in this case.
The above expression is actually equal to:
$x = (true and false);
Why? Because && has a higher precedent than and.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php false place in condition
I have noticed that a lot of PHP code uses conditional statements like CONST == VARIABLE. I grew up with the syntax always articulated in reverse. Is there a reason for this structure?
Example:
// this is the way I see it most typically represented in PHP
if ( false == $foobar ) { // do this }
// this is the way I normally do it
if ( $foobar == false ) { // do this }
This is to prevent a common typo between == and =, known as a yoda condition. Consider the following:
if( false = $foobar) {
This would result in an error, catching what would be considered a bug, since you cannot assign anything to false. On the contrary:
if( $foobar = false) {
This is valid syntax, and is quite an easy mistake to make.
However, I typically prefer the if( $foobar == false) syntax, as unit tests should be able to catch these programmatic mistakes.
The way you normally do it is exactly how most programmers do it. The first example is called a yoda condition:
http://www.dodgycoder.net/2011/11/yoda-conditions-pokemon-exception.html
and is not the norm.