Stupid PHP tricks: (true == false)
otton.org
otton.org
Programmers who use the triple equals in PHP for all comparisons have entirely missed the point.
The reason that looks weird isn't because of the dynamic conversions, but because PHP decided it wanted to look like a statically typed language and added junk like boolean constants. In an implicit typing scenario, you don't have separate constants for different types. In this case, the "booleanness" of the thing is a property of the expression (the == operator, being evaluated inside of an if(), etc...), not the data.
if ($var) { dothis(); }
if (!$var) { dothis(); }
The constants are definitely adding to the confusion. However, only in these contrived examples does one compare numbers and strings with boolean constants. The type conversion is logical, it just looks funny. $x = "";
if ( $x == false ) {} #yes
if ( $x === false ) {} #no
Edit: this is very useful for allowing a string search/manipulation function to return an error condition. $str = string_function(...)
if ( $str === false ) {
#error
} else {
#continue processing $str
}Perl, for example, would write that test as "if(defined $str) {...}".
The point being: strings and booleans are an overlapped type space. If you want something outside that space, the proper choice is not a boolean.
$str = string_function(...);
if ( is_null( $str ) ) {}
if ( null === $str ) {}
Just a matter of preference. $str = '';
$str == null; #true
$str == false; #true
null == false; #true
So in the case of PHP, it's not a matter of correctness, simply because of how PHP handles type coercion. 'false' and 'null' both have legal string values that can evaluate as equal to them or each other.That's the problem. Because they overlap for comparisons, you can't tell an empty string apart from boolean false. It would be safer if it wasn't that way.
But the function returning false is perfectly valid. It's not any different from returning null or some other value because it is distinct from a string.
In PHP, like your Perl example, you could easily write "if (is_bool($str)) {...}" if you wanted to. It's not really any different. The problem is that you don't get an error if you go ahead and treat that boolean as an empty string.
I don't think he was surprised. Based on the post title, I gathered that he was just using that fact to set up an oddity.
The point here seems to be merely that non-empty strings can evaluate to true or false depending on whether they are typecast during the comparison. But that's less of an attention-grabber than true == false, particularly since the latter is patently false in PHP.
Yes, we all instinctively work round PHP's oddities by now, but you've got to admit, "more often than not the operator does exactly what you intend it to" isn't something you want to hear in relation to a programming language. A bit of rigour is no bad thing.
I have a few things I would change:
empty('0') should be false not true
if('0') should be true as well
Comparing a string and int should convert the int to a string not the other way around.
But mostly the rules are logical.No:
'0007' == 7; #true
By your logic: '0007' == 7; #false, wtf?
And why the hell should `empty('0')` return false?! emtpy() determines if the value contains something. I would most certainly think that a string with even one character is not empty, even if it's a space.Right now empty('0') is true - meaning php thinks it's empty (because '0' turns into 0 which turn into false). I think it should be the other way around, and so do you except you read what I wrote backwards.
And I understand the problem of '0007' - but 'abc' == 0 is worse. Most of the time things in php are strings. If you know you'll have leading zeros, then you also know it once was a string, and you'll take measures like intVal. You need intVal to avoid SQL injection attacks anyway.