Re: switch with case 0 [message #182626 is a reply to message #182624] |
Sun, 18 August 2013 22:13 |
Thomas Mlynarczyk
Messages: 131 Registered: September 2010
Karma:
|
Senior Member |
|
|
Norman Peelman schrieb:
> $mode = "fop";
> echo (($mode === "$mode") AND ($mode <= "foo")) ? "True\n" : "False\n";
False. And no surprise. This is more fun:
// Transitivity...?
var_dump( "10" < "foo", "foo" < 2, "10" < 2 );
// Foo is both greater and smaller than 1?
var_dump( "1" == 1, "foo" > "1", "foo" < 1 );
// Appending the same character on both sides...
var_dump( "xe" > "x0", "xe2" > "x02" );
// ... should not change the result
var_dump( "1e" > "10", "1e2" > "102" );
Yes there are rules that PHP follows to create all this madness and it's
all documented. But why wasn't it made as simple and intuitive as in
other languages?
> When you are comparing things, you are asking PHP to look at them...
Yes. But PHP should not turn strings into numbers before doing the
comparison. It should rather turn numbers into strings, if necessary.
> To me that's the same problem in reverse. + is math, . is
> concatination. + needs/wants numbers, . needs/wants strings. You don't
> ADD strings, you JOIN/CONCATINATE them.
Okay, + wants numbers. So "foo" + 2 is an error. Silently turning "foo"
into 0 hides this error instead of slapping it into the programmer's face.
> Ok Thomas, lets think this through. If a string $somevar contains a
> number, you get cast that number. If the cast results in 0 because
> $somevar either does not contain a number or contains "0", does it
> matter?
Yes, I think it does matter. When I'm expecting something that resembles
a number and I get something that does not resemble a number, then
clearly there's something wrong and the situation should be handled in a
special way. There may be cases, where the silent conversion to 0 is
desirable, but the default behaviour should be an error.
> If 0 is not an acceptable result, you the programmer should more
> than likely be coding for that anyway.
Yes, I (as the programmer) should make sure not to try adding "foo" + 2.
But what about my var_dump() examples above? What if I want to compare
"1e2" > "102" as strings and not as numbers (100 > 102)? I'd have to
resort to substr_compare() to achieve that. With PHP, there's usually a
way to get what you want, but it's not always the simplest and most
obvious way. And thus one must be careful with this language to avoid
all the pitfalls. Once I wrote by mistake
if ( strlen( $string <= 64 ) ) // note the misplaced `)`
and it worked like a charm -- for months. Until a had $string values
starting with numbers... Did PHP complain? No. First it did its weird
comparison and then it passed the resulting boolean to strlen() and
strlen() didn't complain, converted the boolean to string, did its
stuff, returned an integer which got converted to a boolean to evaluate
the if condition. If you ever make a mistake and want to hide it -- put
it in a PHP script, it'll be safe there ;-)
Greetings,
Thomas
--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
|
|
|