FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » switch with case 0
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: switch with case 0 [message #182593 is a reply to message #182590] Sat, 17 August 2013 11:16 Go to previous messageGo to previous message
Thomas Mlynarczyk is currently offline  Thomas Mlynarczyk
Messages: 131
Registered: September 2010
Karma:
Senior Member
J.O. Aho schrieb:

> PHP will make a type casting and try to make the "foo" to a integer

And that's precisely what PHP shouldn't do. Cast an integer to a string?
Yes, no problem, since this can be done without loss of information. But
the other way round, especially if the string is obviously not
representing a numeric value? That's just plain stupid.

PHP should never, never, never try to parse out anything from a string
*unless* explicitly told to do so. And throw an error or return `null`
in situations like `(int) "foo"`.

> PHP don't care so much about data
> types so it internally casts it just to make it easy for those who ain't
> so good at programming.

Easy?!? No it doesn't. On the contrary. A "noob" programmer could write
a piece of code like the OPs first example (switch ("foo") ... case 0
....) and rightfully assume it "to work", because in what strange
parallel universe "foo" could possibly equal 0? Even disregarding the
fact that they are of different types. "foo" == 0? WTF?!?

>> switch ($mode) {
>> case SORT_ASC:
>> case SORT_DESC:
>> case SORT_REGULAR:
>> case SORT_NUMERIC:
>> case SORT_STRING:
>> case SORT_LOCALE_STRING:
>> case SORT_NATURAL:
>> case SORT_FLAG_CASE:
>> break;
>> default:
>> throw new \InvalidArgumentException("invalid sort mode: $mode");
>> }

> Sounds like you are trusting user input

No he isn't, as the above code clearly shows. It would work perfectly
well in any normal programming language (although, personally, I would
have used in_array() instead of switch).

Greetings,
Thomas

--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: AND and OR
Next Topic: Compiling PHP 5.x on Cygwin
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 15:07:44 GMT 2024

Total time taken to generate the page: 0.04295 seconds