Literal in front if condition [duplicate] - php

This question already has answers here:
Why does one often see "null != variable" instead of "variable != null" in C#?
(9 answers)
Closed 8 years ago.
Recently I saw programmer put literal in front of if condition
if ( -99 == $poll_id )
Why they did that? What is the difference with normal if e.g.:
if ( $poll_id == -99 )

This an idiom that is borrowed from C. It is called Yoda Conditions.
The idea behind the idiom is avoiding errors when you type one equal sign instead of two.
When you write
if ( x = 3 )
instead of
if ( x == 3 )
The code would compile, but work incorrectly.
If you get in the habit of turning the condition around, the code with a single equal sign would not compile.

Related

PHP - Why does || return 1 while OR returns value? [duplicate]

This question already has answers here:
Logical Operators, || or OR?
(8 answers)
Closed 5 years ago.
I am sending a POST request from my client to the server and fetch the value on the server like this:
$fileName = filter_input(INPUT_POST, "fileName", FILTER_SANITIZE_FULL_SPECIAL_CHARS) or exit("No Filename");
exit($fileName);
Example Output: 'vacation_photo.jpg'
This works fine, however, if I replace or with || then I get 1 as output.
Output: '1'
I was reading this article but I still don't get it.
|| and or have a different precedence. See operator precedence. If make it clear with parentheses you'll get:
fileName = (filter || exit)
(fileName = filter) OR exit
Then the exit in the second line prints true as 1.

What is the benefit of comparing constant and variable with constant on the left? [duplicate]

This question already has answers here:
What is the difference between these (bCondition == NULL) and (NULL==bCondition)? [duplicate]
(6 answers)
Closed 6 years ago.
Can anyone explain what is the benefit of comparing constant and variable with constant on the left e.g.
if (0 == variable)
Instead of
if (variable == 0)
If you say
if (variable == 0)
you are under the risk of omiting one = and saying
if (variable = 0)
which will set the variable variable to 0.
So putting 0 to the left makes it more typo-safe, because 0 = variable won't be able to set any value to the variable.

Conditional in PHP [duplicate]

This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 8 years ago.
When I run the follow code in PHP
if('yes' == 0)
echo 'a';
else
echo 'b';
The output is a.
I don't understand what happen?
And can I convert the php code to C source code to have a look what real happening?
PHP is a dynamically typed language, and == is a loose comparison operator, meaning it will first cast values it compares to one type, int for that matter, and then compare them; strings are being cast to integers by taking numericals from the left part, so 1abc casts to 1. By that logic yes cast to 0, and 0 == 0 yields true.

Why doesn't end(( )) throw a strict notice? [duplicate]

This question already has answers here:
Parentheses altering semantics of function call result
(2 answers)
Closed 8 years ago.
end(array_keys(array(0))) says PHP Strict standards: Only variables should be passed by reference ( http://3v4l.org/CNLVT )
end((array_keys(array(0)))) on the other hand, just works ( http://3v4l.org/168fi ). Why?
The VLD decompiler shows the same opcodes being ran the only difference is in the ext column but I can't find documentation on what that means.
What's likely happening is array_keys is passing the result back by reference. As such, PHP is throwing you a notice that you shouldn't do that.
Wrapping in parenthesis actually changes the reference and forces PHP to evaluate the statement inside first. As such, it removes the reference. One of those weird things that doesn't look like it makes a difference but actually does.
More on the weirdness here http://phpsadness.com/sad/51

What does a single ampersand mean in a PHP conditional statement? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
The following code uses a single & in a conditional check. What does the single ampersand mean there?
if( $some_array_or_other_var & SOME_CONSTANT_VARIABLE ){
//do something here
}
It does not look like a reference, that's what confuses me.
That is a bitwise AND operation: http://www.php.net/manual/en/language.operators.bitwise.php
If, after the bitwise AND, the result is "truthy", the clause will be satisfied.
For example:
3 & 2 == 2 // because, in base 2, 3 is 011 and 2 is 010
4 & 1 == 0 // because, in base 2, 4 is 100 and 1 is 001
This is commonly used to check a single bit in a bitset, by testing powers of two, you are actually checking if a specific bit is set.

Categories