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

Home » Imported messages » comp.lang.php » Switch question
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Switch question [message #184628 is a reply to message #184627] Mon, 13 January 2014 11:53 Go to previous messageGo to previous message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma:
Senior Member
On 01/12/2014 11:10 PM, Norman Peelman wrote:
> On 01/12/2014 11:01 PM, Norman Peelman wrote:
>> On 01/12/2014 04:07 PM, Norman Peelman wrote:
>>> 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.
>>>
>>>
>>
>> Doh! scratch that... doesn't work.
>>
>
> And yes I know why. :) It returns a boolean value. 0 does not match
> but 4 does, "D" also matches. Ha, good catch Denis and Doug!
>

I should state that because *1 || 2 || 3* and *"A" || "B" || "C"*
will return a boolean (TRUE) value that:

0 (FALSE) fails to match as expected, any other number is considered TRUE.
"" and "0" (FALSE) fails to match as expected, any other non-empty
string is considered TRUE.

In reality the CASE expression hasn't been matched at all - not even
when $value contains 1,2 or 3 or "A","B" or "C". Only the boolean state
of TRUE or FALSE has been compared.

Richard, you will need to use the standard SWITCH...CASE syntax. I
overlooked that simple concept even as I typed it in my response.

--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
[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
Previous Topic: signed int64 pack/unpack
Next Topic: Trouble connecting to a PHP SoapServer module
Goto Forum:
  

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

Current Time: Sun Nov 10 09:42:29 GMT 2024

Total time taken to generate the page: 0.05138 seconds