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


Groups > comp.lang.php > #12622

Re: Switch question

From Norman Peelman <npeelman@cfl.rr.com>
Newsgroups comp.lang.php
Subject Re: Switch question
Date 2014-01-12 16:07 -0500
Organization A noiseless patient Spider
Message-ID <lav06t$9ql$1@dont-email.me> (permalink)
References <1jvvn9b20ts0y.1vez76z3aca4b.dlg@40tude.net> <lauiq3$mdc$1@dont-email.me> <1poy7ts4hdy96$.188q6380cr9m0.dlg@40tude.net>

Show all headers | View raw


On 01/12/2014 12:26 PM, richard wrote:
> On Sun, 12 Jan 2014 17:18:59 +0000 (UTC), Denis McMahon wrote:
>
>> On Sun, 12 Jan 2014 11:48:48 -0500, richard wrote:
>>
>>> The manual for PHP switch shows no examples on how to use multiple
>>> values for the same case.
>>> In liberty basic I would use the following:
>>> case 1,2,3,4 case "A","B","C"
>>>
>>> What is the equivelant in PHP?
>>>

   PHP does not support that method directly as you have discovered.

>>> I use the following code to acquire a value:
>>> $value=$_GET['v'];
>>> switch ($value){
>>>    case "A";
>>

   Under normal circumstances we know that $value is a string, therefor 
what is actually in $value is the question. If the code for *case "A"* 
is not being executed then $value <> A.

>> This is f'd up, despite what you may believe ":" is not the same as ";"
>>
>>>    echo "Does Not Work!";
>>>    case A:
>>>    echo "Does Not Work!";
>>> }
>>>
>>> So what is the proper way to ensure the proper case is found correctly?
>>

   The example above would indicate that there is a CONSTANT A that 
$value is compared against. You are mixing problems.


>> You seem to have missed the paragraph beginning "It is important to
>> understand how the switch statement is executed in order to avoid
>> mistakes." on the manual page. This paragraph answers your question.
>
> for once I get to say, to you, >>>> RTFM!
> : is used for constants.
> ; is used for strings.
>

   Not true, the online manual simply indicates that a ; may be used 
instead of a : period. Don't know why, I'd never do it that way - seems 
to add confusion.

> As usuaul, I find the answer to my question.
> Instead of commas, use ||.
>
> case 1 || 2 || 3:
> case "A" || "B" || "C";
>
> The syntax is correct!
>

   There is nothing wrong with that syntax. It is merely an expression 
that ends up being true or not. Using : or ; for either case works as 
tested.


-- 
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


Thread

Switch question richard <noreply@example.com> - 2014-01-12 11:48 -0500
  Re: Switch question Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-12 17:18 +0000
    Re: Switch question richard <noreply@example.com> - 2014-01-12 12:26 -0500
      Re: Switch question Luuk <luuk@invalid.lan> - 2014-01-12 18:44 +0100
      Re: Switch question Evan Platt <evan@theobvious.espphotography.com.invalid> - 2014-01-12 10:47 -0800
        Re: Switch question Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-12 23:15 +0000
      Re: Switch question Norman Peelman <npeelman@cfl.rr.com> - 2014-01-12 16:07 -0500
        Re: Switch question Norman Peelman <npeelman@cfl.rr.com> - 2014-01-12 23:01 -0500
          Re: Switch question Norman Peelman <npeelman@cfl.rr.com> - 2014-01-12 23:10 -0500
            Re: Switch question Norman Peelman <npeelman@cfl.rr.com> - 2014-01-13 06:53 -0500
            Re: Switch question Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-13 13:11 +0000
      Re: Switch question Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-12 23:14 +0000
      Re: Switch question Doug Miller <doug_at_milmac_dot_com@example.com> - 2014-01-13 01:40 +0000
        Re: Switch question The Natural Philosopher <tnp@invalid.invalid> - 2014-01-13 01:54 +0000
          Re: Switch question Doug Miller <doug_at_milmac_dot_com@example.com> - 2014-01-13 03:19 +0000
    Re: Switch question Arno Welzel <usenet@arnowelzel.de> - 2014-01-13 15:34 +0100
  Re: Switch question Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-12 17:22 +0000
    Re: Switch question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-01-12 23:36 +0100
  Re: Switch question Luuk <luuk@invalid.lan> - 2014-01-12 18:21 +0100
  Re: Switch question "Beauregard T. Shagnasty" <a.nony.mous@example.invalid> - 2014-01-12 17:34 +0000
  Re: Switch question Arno Welzel <usenet@arnowelzel.de> - 2014-01-13 15:32 +0100

csiph-web