Reputation: 421
I have inherited some legacy code and I am finding all sorts of weird code. Here's a headscratcher:
if( isset($_POST['name_str']) == 'value of input element' ) {
Here 'name_str' is the name of an input element of type button with a value of 'value of input element'. I understand that isset()
only returns a boolean value. Here the programmer believes the isset() fucntion will evaluate whether the variable exists and what the value is set to. I have never seen this before. Is this a kind of shorthand?
Upvotes: 0
Views: 58
Reputation: 1130
This isn't shorthand, or anything special. But I think it is poor logic.
To explain: The condition loosely compares whether the value of input element is false or true at the same time as 'name_str' being false or true. Since it is only two equals signs, it will not consider the type when comparing each side of the equation, which is a big problem.
This will be truely when $_POST['name_str']
is not set and 'value of input element'
is any of these false condition found here:
the boolean FALSE itself
the integers 0 and -0 (zero)
the floats 0.0 and -0.0 (zero)
the empty string, and the string "0"
an array with zero elements
the special type NULL (including unset variables)
It will also be truely when $_POST['name_str']
is set and 'value of input element' is anything but what is above.
Upvotes: 2
Reputation: 46
It's bad coding, 'value of input element' is a true statement if you run it in an if
if('value of input element') echo 'it will be true';
So if it isset to anything that returns true, logically you have if(true == true)
The string isn't being compared to the $_POST var.
Upvotes: 1
Reputation: 781078
No, it's not shorthand, it's just wrong. The programmer's expectation is incorrect.
I see this mistake frequently in SO questions from beginners, it's hard to believe it makes it into production code.
Upvotes: 2