The difference when using if statement true === something() vs something() === true [duplicate] - php

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.

Related

Trouble using Yoda conditions on PHP IF statement [duplicate]

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.

PHP - order of values in conditions AKA Yoda notation [duplicate]

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.

PHP if statement null === $variable: what for? [duplicate]

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

Advantage of "condition == $variable" style? [duplicate]

This question already has answers here:
PHP why (null === $variable) and not ($variable === null) in comparison? [duplicate]
(3 answers)
Closed 8 years ago.
I often see if's like:
if (null === $var)
I wanted to know wether there is any technical advantage by using it?
I find it semantically wrong and I don't like it. Prefer
if ($var === null)
because I ask for the condition of a variable, not for a variable having a condition.
This type of conditions is sometimes referred to as yoda conditions. Their main advantage is that they avoid accidental assignment:
if ($foo = 'bar')
echo 'Evaluates to true, and reassigns $foo silently';
if ('bar' = $foo)//error
Other than that, it's a matter of personal preference

Order in conditional statements [duplicate]

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.

Categories