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


Groups > comp.lang.php > #10583

Re: switch with case 0

Message-ID <31310115.f6C6Xg6MER@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-08-19 10:18 +0200
Subject Re: switch with case 0
Newsgroups comp.lang.php
References <kumvo8$lel$1@dont-email.me> <b78jt7F1tbkU1@mid.individual.net> <kunm2h$skf$1@news.albasani.net> <kuodhc$645$1@dont-email.me> <kuokfh$qo2$1@news.albasani.net>

Show all headers | View raw


Thomas Mlynarczyk wrote:

> Norman Peelman schrieb:
>>   That's why it says "foo" = 0. Now is "foo" really 0, no - but it sure
>> isn't greater than 0.
> 
> In order to compare "foo" and 0 one would need either to convert the
> number 0 to a string ("0" would be the most reasonable result) or
> convert "foo" to a number (not reasonably possible). PHP, unfortunately,
> chooses the unreasonable alternative.

When you consider the possibility that a request parameter may or may not be 
present, and you wanted an easy way to check this, like

  if ($param)
  {
    // …
  }

it does not look that unreasonable anymore.

The argument can be made that "0" is something, while "" is nothing.  I 
cannot find it reasonable by any measure that "foo" should be equal to 0.  
"foo" certainly is something, while at the same time the argument can be 
made that 0 is a mathematical “nothing” (the neutral element towards 
addition).

But PHP, after all, also allows for strict comparison to avoid this problem.  
Why do you not use it, and then complain about the language (with dubious 
assertions about it, BTW)?

  if ($param && $param !== "0")
  {
    // …
  }
 
>> PHP looks at the string in question one character
>> at a time and builds a number until a non-numeric character is reached
>  
> I am aware of all that. It was decided to make PHP work that way. I was
> merely pointing out that I consider it a very unfortunate decision to
> cast strings to numbers instead of the other way round.

I think it is the natural result of PHP being the *P*HP *H*ypertext 
*P*reprocessor.
 
BTW, because of discussions like this one, switch-case has been either 
removed in an early version from or is never going to be introduced into 
Python, another programming language that uses dynamic type-checking (we 
should lose the term “loose typing”, it is too loose to be useful).  You 
have to write

  if foo == 1:
      pass
  elif foo == 2:
      pass
  elif foo == 3:
      pass
  else:
      pass

and that is precisely what you should do in PHP if the type matters:

  if ($foo === 1)
  {
    // …
  }
  else if ($foo === 2)
  {
    // …
  }
  else if ($foo === 3)
  {
    // …
  }
  else
  {
    /* default */
  }


PointedEars
-- 
var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
)  // Plone, register_function.js:16

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