Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #10543

Re: switch with case 0

From "J.O. Aho" <user@example.net>
Newsgroups comp.lang.php
Subject Re: switch with case 0
Date 2013-08-17 08:35 +0200
Message-ID <b78jt7F1tbkU1@mid.individual.net> (permalink)
References <kumvo8$lel$1@dont-email.me>

Show all headers | View raw


On 17/08/13 06:55, Fiver wrote:
> This example
>
>    switch ("foo") {
>      case 0:
>        echo "'foo' is 0\n";
>        break;
>      default:
>        echo "'foo' is not 0\n";
>    }
>
> will print: 'foo' is 0.

The switch you are doing is equal to a

if("foo" == 0) {}

PHP will make a type casting and try to make the "foo" to a integer, as 
it's not a string only of numeric characters and those it will be cast 
to a 0 which makes your statement true.


You can try this one:

switch("123") {
	case 0:
		echo "it's 0\n";
	  break;

	case 123:
		echo "it's 123\n";
	  break;

	default:
		echo "it's NOT 0 nor 123\n";
}


> I know how to avoid the problem, I'm just wondering what kind of
> comparison is used that makes "foo" equal to 0? The manual mentions
> "loose comparison", but this is even looser than usual.

No it's not looser than normally, 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.


> By the way, the original code was meant to validate SORT_* arguments.
> It's a little harder to spot here:
>
>    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");
>    }
>
> SORT_REGULAR is 0, so if the input is a string, this will break (literally).

Sounds like you are trusting user input, always validate the user input 
to be a valid value of the type you are requiring. In this case 
is_numeric() could save you from trouble.

-- 

  //Aho

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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