Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #10559
| From | Norman Peelman <npeelman@cfl.rr.com> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: switch with case 0 |
| Date | 2013-08-17 13:56 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <kuodhc$645$1@dont-email.me> (permalink) |
| References | <kumvo8$lel$1@dont-email.me> <b78jt7F1tbkU1@mid.individual.net> <kunm2h$skf$1@news.albasani.net> |
On 08/17/2013 07:16 AM, Thomas Mlynarczyk wrote:
> 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.
>
That's why it says "foo" = 0. Now is "foo" really 0, no - but it sure
isn't greater than 0. PHP looks at the string in question one character
at a time and builds a number until a non-numeric character is reached
(in a nutshell):
"foo" = 0
"foo2you" = 0
"2fooyou" = 2
"250fooyou" = 250
"250.0fooyou" = 250.0
The (basic) premise is a string can only result in a number if it
starts with a number, otherwise not. If you want to know why "foo" = 0
then, it's the same as doing math with NULL. NULL <> 0 but acts like it
in computations:
2 * "2fooyou" = 4
> 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"`.
An empty string "" = 0, "foo" does not lead with any *numbers* so
therefor is = 0. Saying (int) "foo" = NULL is like saying "foo" isn't there.
>
>> 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).
>
You are assuming $mode is never a string for one. For two, type
juggling is done at the CASE statement, not the SWITCH statement.
--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
switch with case 0 Fiver <numeralfive@gmail.com> - 2013-08-17 06:55 +0200
Re: switch with case 0 "J.O. Aho" <user@example.net> - 2013-08-17 08:35 +0200
Re: switch with case 0 The Natural Philosopher <tnp@invalid.invalid> - 2013-08-17 08:29 +0100
Re: switch with case 0 Luuk <luuk@invalid.lan> - 2013-08-17 09:51 +0200
Re: switch with case 0 Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2013-08-17 13:16 +0200
Re: switch with case 0 "Peter H. Coffin" <hellsop@ninehells.com> - 2013-08-17 10:56 -0500
Re: switch with case 0 The Natural Philosopher <tnp@invalid.invalid> - 2013-08-17 17:45 +0100
Re: switch with case 0 Tim Streater <timstreater@greenbee.net> - 2013-08-17 18:58 +0100
Re: switch with case 0 The Natural Philosopher <tnp@invalid.invalid> - 2013-08-17 19:39 +0100
Re: switch with case 0 Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2013-08-17 21:36 +0200
Re: switch with case 0 Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2013-08-17 22:14 +0200
Re: switch with case 0 Norman Peelman <npeelman@cfl.rr.com> - 2013-08-17 13:56 -0400
Re: switch with case 0 Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2013-08-17 21:55 +0200
Re: switch with case 0 The Natural Philosopher <tnp@invalid.invalid> - 2013-08-17 21:04 +0100
Re: switch with case 0 Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2013-08-17 22:32 +0200
Re: switch with case 0 Richard Yates <richard@yatesguitar.com> - 2013-08-17 15:20 -0700
Re: switch with case 0 Fiver <numeralfive@gmail.com> - 2013-08-17 22:37 +0200
Re: switch with case 0 Norman Peelman <npeelman@cfl.rr.com> - 2013-08-17 19:26 -0400
Re: switch with case 0 The Natural Philosopher <tnp@invalid.invalid> - 2013-08-18 12:54 +0100
Re: switch with case 0 Norman Peelman <npeelman@cfl.rr.com> - 2013-08-17 20:23 -0400
Re: switch with case 0 Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2013-08-18 15:22 +0200
Re: switch with case 0 Norman Peelman <npeelman@cfl.rr.com> - 2013-08-18 11:20 -0400
Re: switch with case 0 Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2013-08-18 18:28 +0200
Re: switch with case 0 Norman Peelman <npeelman@cfl.rr.com> - 2013-08-18 15:16 -0400
Re: switch with case 0 Norman Peelman <npeelman@cfl.rr.com> - 2013-08-18 18:04 -0400
Re: switch with case 0 Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2013-08-19 00:17 +0200
Re: switch with case 0 Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2013-08-19 00:13 +0200
Re: switch with case 0 The Natural Philosopher <tnp@invalid.invalid> - 2013-08-19 00:35 +0100
Re: switch with case 0 Norman Peelman <npeelman@cfl.rr.com> - 2013-08-19 06:30 -0400
Re: switch with case 0 Norman Peelman <npeelman@cfl.rr.com> - 2013-08-18 22:13 -0400
Re: switch with case 0 Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2013-08-19 19:55 +0200
Re: switch with case 0 Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-19 10:18 +0200
Re: switch with case 0 Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2013-08-19 19:39 +0200
Re: switch with case 0 Fiver <numeralfive@gmail.com> - 2013-08-17 16:15 +0200
Re: switch with case 0 Norman Peelman <npeelman@cfl.rr.com> - 2013-08-17 13:28 -0400
csiph-web