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.
Related
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:
Short-circuit evaluation via the AND operator in PHP
(5 answers)
Closed 6 years ago.
This is just more of a "why does it work" and "why or why not use it" type question.
We all know PHP expressions using ternary operations
$var = (isset($i) ? true:false);
But in PHP something like the following works too. (It is not ternary, 3 parts, it is more of a binary operation, 2parts.)
$var = true;
isset($i) || $var = false;
Which may not be so practical :) but a more useful construction is
isset($i) || exit();
So the above (much better looking imo) would have the same result as
if(!isset($i)) exit();
But other than the common
defined('CONSTANT') || define('CONSTANT','FOO');
I rarely see this type of construct used in PHP. Why does this work? What is it called. Why is it not taught or used more. Are there cons to using it? And is there practical ways to use && in the same way?
This way of writing statements also exists in C, Perl and other languages, it plays with how the compiler evaluates statements chained with logical operators.
Imagine you have an expression if (a() or b()), then when a() returns true you don't have to evaluate b(), because the entire statement is true already. So b() is only called when a() is false. You can write this without the if, it still works the same way.
This shorthand is usually found like you described, to define default values or exit if a condition is not met.
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:
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
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
The 3 different equals
Can anyone tell me why, when using the code below, I am getting redirected to elephant.com rather than seeing a 'giraffe!
<?php
$foo="giraffe";
if($foo="elephant"){
header("location:http://www.elephant.com");
exit();
}else{
echo $foo;}
?>
Thanks for looking
J
if($foo="elephant")
You're assigning $foo here, rather than comparing it; you should be doing:
if($foo=="elephant")
The result of an assignment operation is the value that's just been assigned; in this case, 'elephant' is evaluating to true.
Your if() statement has a single equal sign. This doesn't do a comparison in PHP; it sets the value and returns true.
In order to do a comparison, you need to use either a double-equal or a triple-equal sign:
if($foo == "elephant") { .... }
or
if($foo === "elephant") { .... }
The difference between the two is that double-equal doesn't care about the variable's data type, whereas triple-equal does. In this case, there's not much difference between them, but it's worth learning and understanding the differences because they can bite you if you don't know them. More info here: http://php.net/manual/en/language.operators.comparison.php