Switch question [message #184599] |
Sun, 12 January 2014 16:48  |
Mr Oldies
Messages: 241 Registered: October 2013
Karma: 0
|
Senior Member |
|
|
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?
I use the following code to acquire a value:
$value=$_GET['v'];
switch ($value){
case "A";
echo "Does Not Work!";
case A:
echo "Does Not Work!";
}
So what is the proper way to ensure the proper case is found correctly?
|
|
|
Re: Switch question [message #184600 is a reply to message #184599] |
Sun, 12 January 2014 17:18   |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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?
>
> I use the following code to acquire a value:
> $value=$_GET['v'];
> switch ($value){
> case "A";
This is fucked 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?
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.
--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
|
|
|
|
|
Re: Switch question [message #184603 is a reply to message #184600] |
Sun, 12 January 2014 17:26   |
Mr Oldies
Messages: 241 Registered: October 2013
Karma: 0
|
Senior Member |
|
|
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?
>>
>> I use the following code to acquire a value:
>> $value=$_GET['v'];
>> switch ($value){
>> case "A";
>
> This is fucked 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?
>
> 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.
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!
|
|
|
|
|
|
Re: Switch question [message #184607 is a reply to message #184603] |
Sun, 12 January 2014 21:07   |
Norman Peelman
Messages: 126 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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?-
|
|
|
|
Re: Switch question [message #184613 is a reply to message #184603] |
Sun, 12 January 2014 23:14   |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Sun, 12 Jan 2014 12:26:01 -0500, 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?
>>>
>>> I use the following code to acquire a value:
>>> $value=$_GET['v'];
>>> switch ($value){
>>> case "A";
>>
>> This is fucked 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?
>>
>> 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.
Yes, I wasn't aware that ; could be used instead of :, however you fucked
up when you said ": is used for constants. ; is used for strings."
Nice to see you're still on form with the fuck-ups.
> 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!
The syntax may be valid php syntax, but I'll bet any money you like that
the code doesn't do what you expect under all operating conditions,
because you have fucked up yet again.
Here is a simple program which demonstrates your fuck-up:
<?php
function test($x){
echo "testing {$x} for \"fred\", \"jim\" or \"sue\"\n";
switch($x){
case "fred"||"jim"||"sue":echo" match\n";break;
default;echo" no match\n";break;
}
}
$names = array("pete","fred","alan","sue","andy","jim","hank","richard ");
foreach($names as $name)test($name);
--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
|
|
|
Re: Switch question [message #184614 is a reply to message #184606] |
Sun, 12 January 2014 23:15   |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Sun, 12 Jan 2014 10:47:55 -0800, Evan Platt wrote:
> On Sun, 12 Jan 2014 12:26:01 -0500, richard <noreply(at)example(dot)com>
> wrote:
>
>> for once I get to say, to you, >>>> RTFM!
>
> You tried that before. You were wrong again then, too.
Actually he was right all the way up to the exclamation mark on that
line, but then he reverted to traditional richard.
--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
|
|
|
|
|
|
Re: Switch question [message #184626 is a reply to message #184607] |
Mon, 13 January 2014 04:01   |
Norman Peelman
Messages: 126 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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.
--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
|
|
|
Re: Switch question [message #184627 is a reply to message #184626] |
Mon, 13 January 2014 04:10   |
Norman Peelman
Messages: 126 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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!
--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
|
|
|
Re: Switch question [message #184628 is a reply to message #184627] |
Mon, 13 January 2014 11:53   |
Norman Peelman
Messages: 126 Registered: September 2010
Karma: 0
|
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?-
|
|
|
|
|
Re: Switch question [message #184631 is a reply to message #184600] |
Mon, 13 January 2014 14:34  |
Arno Welzel
Messages: 317 Registered: October 2011
Karma: 0
|
Senior Member |
|
|
Am 12.01.2014 18:18, schrieb Denis McMahon:
> 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?
>>
>> I use the following code to acquire a value:
>> $value=$_GET['v'];
>> switch ($value){
>> case "A";
>
> This is fucked up, despite what you may believe ":" is not the same as ";"
Nope - unusual - but valid.
According to <http://php.net/manual/en/control-structures.switch.php>, cite:
It's possible to use a semicolon instead of a colon after a case like:
<?php
switch($beer)
{
case 'tuborg';
case 'carlsberg';
case 'heineken';
echo 'Good choice';
break;
default;
echo 'Please make a new selection...';
break;
}
?>
--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
|
|
|