What's the difference between those PHP if expressions? - php

What's the difference between those PHP if expressions!?
if ($var !== false)
{
// Do something!
}
if (false !== $var)
{
// Do something!
}
Some frameworks like Zend Framework uses the latest form while the traditional is the first.
Thanks in advance

The result of the expression is the same however it's a good way of protecting yourself from assigning instead of comparing (e.g writing if ($var = false) instead of if ($var == false), since you can't assign the value $var to the false keyword)

It's just a preference really. You can put it either way, a == b or b == a, but it's easier to make a mistake if you do
if ($var == false)
because if you accidentally type it with one = letter, the condition will always equal to true (because $var will be set successfully), but in case of
if (false == $var)
if you now put in =, you will get an error.

The two expressions are semantically identical. It's just a matter of preference whether to put the constant expression first or last.

There's no real difference. The operands are just on different sides but the comparison is the same nonetheless.

There is no difference. !== compares 2 expressions with type checking, and returns true if they are not equal. The difference may be in the evaluation order of the expressions, but in the case you wrote, there is no difference (and a good program must not rely on the execution order of such expressions).

Consider this scenario:
if (strpos($string, $substring)) { /* found it! */ }
If $substring is found at the exact start of $string, the value returned is 0.
Unfortunately, inside the if, this evaluates to false, so the conditional is not executed.
The correct way to handle that will be:
if (false !== strpos($string, $substring)) { /* found it! */ }
Conclusion:
false is always guaranteed to be false. Other values may not guarantee this. For example, in PHP 3, empty('0') was true, but it changed to false later on.

Related

Why do some people put the value before variable in if statement?

For example, what's different from $variable === true?
<?php
if (true === $variable) {
//
}
if (1 === intval($variable)) {
//
}
They are equivalent.
Some programmers prefer this "Yoda style" in order to avoid the risk of accidentally writing:
if ($variable = true) {
// ...
}
, which is equivalent to
$variable = true;
// ...
, when they meant to write
if ($variable === true) {
// ...
}
(whereas if (true = $variable) would generate an obvious error rather than a potentially-subtle bug).
Short answer
Some people do it in order to avoid mistakenly using the assignment operator (=) when they really meant to use a comparison operator (== or ===).
Long answer
In PHP there are 3 operators that can be mistaken for eachother:
= is the assignment operator
== is the "equal to" operator
=== is the "identical to" operator
The first operator is only used for assigning a value to a variable. The second and third are only used for comparing two values, or expressions, against eachother.
In PHP it is possible to assign a value to a variable inside control structures (if, for, while, etc.). This may cause problems if you are not careful. For example, if you want to compare $a against a boolean value you can do it like this:
if ($a == false) // or
if ($a === false)
If you are not careful, however, it may end up like this:
if ($a = false)
... which is perfectly legal code, but it is an assignment and will eventually cause problems. The reason it will cause problems is because the expression, $a=false, will be evaluated and the code will keep running as if nothing is wrong, when in fact it was not intended.
The reason some people switch around the operands, when comparing a value against a literal (fixed value, like true, false, 7, 5.2, and so on), is because it is not possible to assign a value to a literal. For example:
if (false = $a)
... will cause an error, telling the programmer that they made a mistake, which they can then fix.
I guess this is just the way of thinking in general. When you really think about a deep if statement, you think if that statement is true, not the other point of view. It's not wrong, but in my head naming it inverse, would annoy me and make me lose concentration about the statement. So I would say it's just the way people think :D.
There is no difference between ($var === true) and (true === $var). They are equivalent.
See http://php.net/manual/en/types.comparisons.php for a complete table of comparisons. You'll see that all equivalent comparisons have the same result.
Also, some people do prefer to see what's the result which is been evaluated before the statement.
Some statements might be longer and harder to read.
Ex:
if (false == (new DataParser()->makeAComplexDataParser((new SomeTransformer()->doTransformation((new someClass()->getMethodOfLongParameters($param1, $param2, (new Date()->format('Y-m-d')))))))) ) {
// do stuff
}
So it's better to think "is it false this complex expression?" instead of thinking
"this looooooooonger complex expression is....hmmmm....false?"

What's the difference between 'false === $var' and '$var === false'?

Is one more readable than the other? At first, I disliked the false === approach but as I see it more and more often, I'm warming up to it. I'm pretty sure they return identical results.
There's absolutely no functional difference.
I usually prefer to place the variable first, and the constant value second. Because that makes sense (When you speak it out loud, would you say "I test that the variable is false"? Or "I test that false is equal to the variable"?)
I greatly prefer
false === $var
Namely because sometimes you are only using equality and not looking for identity.
In which case you write
false == $var
But sometimes you aren't at the top of your game, and might write
false = $var
which will give an immediate error, and let's you fix it right away.
However, if you type
$var = false
you end up beating your head against a wall for an hour trying to figure out why your script isn't working right.
A much better software engineer than me taught me about this. Long story short, placing the constant first is a best practice, although it may look weird at first. I say that it's a best practice because it results in the most predictable outcomes:
$var = null;
if (false === $var) { // NullPointerException is not thrown; only evaluates to "true" if the value "false" was assigned to $var
...
}
if ($var === false { // NullPointerException will be thrown
...
}
if (false = $var) { // Parse Error
...
}
if ($var = false) { // the value false is assigned to $var
...
}

if statement value position

What is the difference between the 2 statements:
if (false === $variable) {
//do something
}
and
if ($variable === false) {
//do something
}
I personally use the second style but often run into code in frameworks that i use which always seem to use the first style.
Is there a difference ( i suspect it is some legacy thing to do with types) or is this simply a coding habit (it must be rooted in something though??)
If not, what is the reasoning behind the first style given that the logic is actually backwards.
if ($foo = false) (note: = instead of ==) is a typical typo that leads to hard to debug problems. For that reason, some people prefer false == $foo, since the runtime will throw an error when you try to assign something to false.
Other than that there's no difference.
If you put the value that can't have something assigned to it (a literal, a constant, a function call, etc) on the left hand side then, if you accidentally use an assignment operator (=) instead of a comparison operator, then you'll get an error rather then a hard-to-track-down bug.
i.e.
if (false = $variable) { // Error
if ($variable = false) { // Valid code that doesn't do what you want
This dates to C programming. It's a way to make sure "compilers" will fail if you accidently used = (assignment) and not == or === (equals) because "false" is not an lvalue (can't be on the left side of an assignment)
It stems from this:
if (false = $variable)
syntax error
if ($variable = false)
accidental assignment
It has been around for a while. It comes from accidentally writing (by forgetting one =):
if (c = 1) {
}
which does assignment and it's doesn't actually check whether c equals 1. It assigns 1 and then it evaluates to 1 (so the if statement becomes always true due to that mistake). By writing
if (1 == c) {
}
as a habit, you cannot make that mistake. If you would forget the = in this case, the compiler would warn you because assignment to the literal 1 is not possible.
Practically, the two statements are equivalent.
So, why to write it this way or the other (?) :
Try to avoid a typical typo : if ($condition = false) instead of if ($condition == false). In that case, your 2nd version would cause a difficult-to-debug issue, while the first would throw an error
Want to force one of the two parts of the conditional statement be evaluated first (the rightmost)

Processing of php 'if x && y'

I was wondering how php processes if statements.
If i were to have something such as:
if (isset($_GET['foo']) && $_GET['foo'] != $bar)
If foo isn't set, would it then drop out of the if straight away (as it is an 'and' statement so it can't succeed anyway) or would it also check the second part, rather pointlessly?
What you're describing is known as "short-circuit evaluation".
Most languages work this way, including PHP, so they will evaluate an expression until they are certain of the result, and then stop, so the remainder of the expression would not be evaluated.
As you say, this is the most efficient approach.
However, it can potentially throw a spanner in the works for inexperienced programmers, who nay try something like this:
if(doFirstProcess() && doSecondProcess() {
print "both processes succeeded";
}
In this case, the programmer is expecting both functions to be called, but if the first one returns false, then the second one will not be executed, as the program already knows enough to be certain of the final result of the expression, so it short-circuits the remainder of the expression.
There are a few languages which don't do short-circuit evaluation. VB6 was one example (back in the day). I don't know about VB.Net, but since it's evolved from VB6, I would suspect it would be similar. But aside from that, all other languages that I've worked with have used short-circuit evaluation, including PHP.
There is a section in the PHP manual about this here: http://www.php.net/manual/en/language.operators.logical.php
And you can read more on short circuit evalution here: http://en.wikipedia.org/wiki/Short-circuit_evaluation
Hope that helps.
It's known as short-circuit:
&& and and operators is executed from left to side
If left side is considered false, no reasons to check right side, so it's omitted, false returned
|| and or operators is executed from left to side too
If left side is considered true, no reasons to check right side, so it's omitted, true returned
Manual example:
// foo() will never get called as those operators are short-circuit
$a = (false && foo());
$b = (true || foo());
$c = (false and foo());
$d = (true or foo());
it will leave the if statement after the first expression evaluates to false because this statement can never be true if the first one is false and they are combinded via AND
you can check this very easily. If it wouldn't be like I said, you would get a notice that $_GET['foo'] is not defined
If the first part is false, it stops the if.
Certain operators, most notably && and || are so-called short-circuit operators, meaning that if the result of the operation is clear from the first operand (false or true, respectively), the second operand does not get evaluated.
Edit: Additionally, operands are guaranteed to be evaluated in order, this is not always true of other operators.
Will go out after the first statement.
you can test it:
will print 1:
if(1==1 && $a=1 == 1){
}
print $a;
Will not print a thing:
if(1==2 && $a=1 == 1){
}
print $a;
&& does short-circuit (i.e. returns false as soon as one condition fails).
If it doesn't, then having the isset would be pointless — it exists to prevent errors when trying to compare an undefined value to a string.
If the first check if (isset($_GET['foo']) returns false, the second part will not even be looked into anymore.

PHP - and / or keywords

Is && the same as "and", and is || the same as "or" in PHP?
I've done a few tests, and it seems they behave the same. Are there any differences?
If not, are there any other PHP signs that have word equivalents and do you think it makes the code easier to read?
and and or have higher lower precedence than && and ||. To be more exact && and || have higher precedence than assignment operator ( = ) while and and or have lower.
http://www.php.net/manual/en/language.operators.precedence.php
Usually it doesn't make a difference, but there are cases when not knowing about this difference can cause some unexpected behaviour. See examples here:
http://php.net/manual/en/language.operators.logical.php
Yes, they are logically the same. (I believe "&&" and "||" are the preferred choice in the Zend coding standards, but I can't find any specific information on this, so it might all have been a dream. Or something.)
That said:
"&&" and "||" are of a higher precedence than "AND" and "OR" (unlikely to ever be relevant, but you never know).
A lot of other languages use "&&" and "||", rather than the textual equivalents so it might be an idea to go with this.
As long as you use your choosen set of operators consistently it doesn't really matter.
What bothers me is:
echo (false and false ? true : true);
// (empty/false)
You might guess there is only the possible output of "1" (true) as there is no case which could output a false... ...but it will be "" (false).
Using && as a operator in this case satifies at least my expectations:
echo (false && false ? true : true);
// 1
So, in some cases the usage matters significantly.
The difference is on the precedence. But not only compared with each other!
In most cases you won't mind it, but there are specific cases when you have to take one step back and look at the big picture. Take this, for example:
// The result of the expression (true && false) is assigned to $g
// Acts like: ($g = (true && false))
$g = true && false;
// The constant true is assigned to $h before the "and" operation occurs
// Acts like: (($h = true) and false)
$h = true and false;
var_dump($g, $h);
This will produce, respectively:
bool(false)
bool(true)
In other words, && has higher preference than =, which has higher precedence than and, as stated in http://php.net/manual/en/language.operators.precedence.php. (It is mentioned in other answers, but I think it's worth to detail, since a misuse can lead to logical errors)
I hope it may help you. You can find more at http://php.net/manual/en/language.operators.logical.php

Categories