Assignment in PHP with bool expression: strange behaviour [duplicate] - php

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.

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.

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

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.

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.

How about method of var_dump($x) in php [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
By the way i surprised about this
What will this code output and why?
$x = true and false;
var_dump($x);
Surprisingly to many, the above code will output bool(true) seeming to imply that the and operator is behaving instead as an or.
The issue here is that the = operator takes precedence over the and operator in order of operations, so the statement $x = true and false ends up being functionally equivalent to:
$x = true; // sets $x equal to true
true and false; // results in false, but has no affect on anything
This is, incidentally, a great example of why using parentheses to clearly specify your intent is generally a good practice, in any language. For example, if the above statement $x = true and false were replaced with $x = (true and false), then $x would be set to false as expected.
As explained here, the = operator has higher precedence than the and operator, causing $x = true to evaluate before true and false does, meaning that $x will take the value of true.
This will give you what you want:
$x = (true and false);

Where can I read about conditionals done with "?" and ":" (colon)? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
I've been doing conditionals with if/else or a year or so now. Looking at some new code, I'm seeing a conditional that appears to use ? and : instead of if and else. I'd like to learn more about this, but I am not sure what to google to find articles explaining how it works. How can I do it?
It's the Ternary Operator.
Basic usage is something like
$foo = (if this expressions returns true) ? (assign this value to $foo) : (otherwise, assign this value to $foo)
It can be used for more than assignment though, it looks like other examples are cropping up below.
I think the reason you see this in a lot of modern, OO style PHP is that without static typing you end up needing to be paranoid about the types in any particular variable, and a one line ternary is less cluttered than a 7 line if/else conditional.
Also, in deference to the comments and truth in naming, read all about the ternary operators in computer science.
That would be the conditional operator. It's pretty much a single line if/then/else statement:
if(someCondition){
$x = doSomething();
}
else{
$x = doSomethingElse();
}
Becomes:
$x = someCondition ? doSomething() : doSomethingElse();
It is:
condition ? do_if_true : do_if_false
So, for example in the below, do->something() will be run.
$true = 1;
$false = 0
$true ? $do->something() : $do->nothing();
But in the below example, do->nothing() will be run.
$false ? $do->something() : $do->nothing();
This is the ternary operator in PHP. It's shorthand for if/else, format is:
condition ? true expression : false expression;

Categories