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
Switch to threaded view of this topic Create a new topic Submit Reply
Switch question [message #184599] Sun, 12 January 2014 16:48 Go to next message
Mr Oldies is currently offline  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 Go to previous messageGo to next message
Denis McMahon is currently offline  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 #184601 is a reply to message #184599] Sun, 12 January 2014 17:22 Go to previous messageGo to next message
Denis McMahon is currently offline  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.

Oh, and just to clarify, you also need to add "I am incapable of
reading." and "I tell lies." to your cv as well.

There is a very clear example on the manual page of matching against
multiple values to execute a single set of statements.

Does this mean we're going to spend the next 5 years debugging your
fucked up switch statements?

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: Switch question [message #184602 is a reply to message #184599] Sun, 12 January 2014 17:21 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 12-01-2014 17:48, richard wrote:
> The manual for PHP switch shows no examples on how to use multiple values
> for the same case.

I think this is a pretty neat example:
<?php
switch ($i) {
case 0:
case 1:
case 2:
echo "i is less than 3 but not negative";
break;
case 3:
echo "i is 3";
}
?>
found on this page:
http://www.php.net/manual/en/control-structures.switch.php

> In liberty basic I would use the following:

this should be about PHP, not about liberty, or about basic....
Re: Switch question [message #184603 is a reply to message #184600] Sun, 12 January 2014 17:26 Go to previous messageGo to next message
Mr Oldies is currently offline  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 #184604 is a reply to message #184599] Sun, 12 January 2014 17:34 Go to previous messageGo to next message
Beauregard T. Shagnas is currently offline  Beauregard T. Shagnas
Messages: 154
Registered: September 2010
Karma: 0
Senior Member
richard the sto0pid wrote:

[about switch]

> The manual for PHP switch shows no examples on how to use multiple
> values for the same case.
> In liberty basic

So why not write your site in that language?

> I would use the following:
> case 1,2,3,4 case "A","B","C"
>
> What is the equivelant in PHP?

It's going to be similar. Above, you've not mixed string and numeric; why
would you do it below?

> I use the following code to acquire a value:
> $value=$_GET['v'];
> switch ($value){
> case "A"; <------- string
> echo "Does Not Work!";
> case A: <------- numeric (or.. not really anything!)
> echo "Does Not Work!";
> }

You've forgotten break and default, and how would you know which was
chosen if both echo statements are identical?

> So what is the proper way to ensure the proper case is found correctly?

switch($nmonth) {
case "01": $cmonth="January"; break;
case "02": $cmonth="February"; break;
case "03": $cmonth="March"; break;
case "04": $cmonth="April"; break;
case "05": $cmonth="May"; break;
case "06": $cmonth="June"; break;
case "07": $cmonth="July"; break;
case "08": $cmonth="August"; break;
case "09": $cmonth="September"; break;
case "10": $cmonth="October"; break;
case "11": $cmonth="November"; break;
case "12": $cmonth="December"; break;
default: $cmonth="Not a valid month number"; break;
}
echo $cmonth;

You should find a new hobby.

--
-bts
-This space for rent, but the price is high
Re: Switch question [message #184605 is a reply to message #184603] Sun, 12 January 2014 17:44 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 12-01-2014 18:26, 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:
>>
>>>
>>> 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.
>

From the manual:
"It's possible to use a semicolon instead of a colon after a case like:"

But this has nothing to do with contants or strings, or....?
Re: Switch question [message #184606 is a reply to message #184603] Sun, 12 January 2014 18:47 Go to previous messageGo to next message
Evan Platt is currently offline  Evan Platt
Messages: 124
Registered: November 2010
Karma: 0
Senior Member
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.
--
To reply via e-mail, remove The Obvious and .invalid from my e-mail address.
Re: Switch question [message #184607 is a reply to message #184603] Sun, 12 January 2014 21:07 Go to previous messageGo to next message
Norman Peelman is currently offline  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 #184611 is a reply to message #184601] Sun, 12 January 2014 22:36 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Denis McMahon wrote:

> Does this mean we're going to spend the next 5 years debugging your
> fucked up switch statements?

I should have known that “richard” would find a way to use this
for his trolling, so I should not have suggested the possibility
in <news:4555789(dot)NIToQkNJ70(at)PointedEars(dot)de>. Sorry, my bad.


PointedEars
--
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
-- Richard Cornford, comp.lang.javascript, 2011-11-14
Re: Switch question [message #184613 is a reply to message #184603] Sun, 12 January 2014 23:14 Go to previous messageGo to next message
Denis McMahon is currently offline  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 Go to previous messageGo to next message
Denis McMahon is currently offline  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 #184619 is a reply to message #184603] Mon, 13 January 2014 01:40 Go to previous messageGo to next message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
richard <noreply(at)example(dot)com> wrote in news:1poy7ts4hdy96$.188q6380cr9m0.dlg@
40tude.net:

>
> case 1 || 2 || 3:
> case "A" || "B" || "C";
>
> The syntax is correct!
>
<snicker>
The result will not always be what you expect.
Re: Switch question [message #184620 is a reply to message #184619] Mon, 13 January 2014 01:54 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 13/01/14 01:40, Doug Miller wrote:
> richard <noreply(at)example(dot)com> wrote in news:1poy7ts4hdy96$.188q6380cr9m0.dlg@
> 40tude.net:
>
>>
>> case 1 || 2 || 3:
>> case "A" || "B" || "C";
>>
>> The syntax is correct!
>>
> <snicker>
> The result will not always be what you expect.
>
Richard has very low expectations...

--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
Re: Switch question [message #184622 is a reply to message #184620] Mon, 13 January 2014 03:19 Go to previous messageGo to next message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
The Natural Philosopher <tnp(at)invalid(dot)invalid> wrote in news:lavgvu$6cp$1
@news.albasani.net:

> On 13/01/14 01:40, Doug Miller wrote:
>> richard <noreply(at)example(dot)com> wrote in news:1poy7ts4hdy96$.188q6380cr9m0.dlg@
>> 40tude.net:
>>
>>>
>>> case 1 || 2 || 3:
>>> case "A" || "B" || "C";
>>>
>>> The syntax is correct!
>>>
>> <snicker>
>> The result will not always be what you expect.
>>
> Richard has very low expectations...

You misspelled "abilities".

I think RtS actually has very high expectations, not only of himself -- witness his dauntless
attempts at problems that are clearly beyond his grasp -- but also of the other members of
this newsgroup, whom he seems to believe are under some sort of obligation to fix all his
errors for him.

I propose that we all agree that the next time RtS insults someone who is trying to help him,
we will all ignore everything he posts until he apologizes.
Re: Switch question [message #184626 is a reply to message #184607] Mon, 13 January 2014 04:01 Go to previous messageGo to next message
Norman Peelman is currently offline  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 Go to previous messageGo to next message
Norman Peelman is currently offline  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 Go to previous messageGo to next message
Norman Peelman is currently offline  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 #184629 is a reply to message #184627] Mon, 13 January 2014 13:11 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Sun, 12 Jan 2014 23:10:14 -0500, Norman Peelman wrote:

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

To be honest, the trouble with pointing out glaring errors in richardcode
is working out which fish to shoot, because the barrel is so overcrowded.

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: Switch question [message #184630 is a reply to message #184599] Mon, 13 January 2014 14:32 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
Am 12.01.2014 17:48, schrieb richard:

> The manual for PHP switch shows no examples on how to use multiple values
> for the same case.

1) Go to <http://php.net/manual/en>

2) Use the "Search" box to look for "switch"

3) You should find this:

<http://php.net/manual/en/control-structures.switch.php>

Cite:

The statement list for a case can also be empty, which simply passes
control into the statement list for the next case.

<?php
switch ($i) {
case 0:
case 1:
case 2:
echo "i is less than 3 but not negative";
break;
case 3:
echo "i is 3";
}
?>

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

switch($value)
{
case 1:
case 2:
case 3:
// dosomething
break; // <--- THIS IS IMPORTANT!

case A:
case B:
case C:
// dosomethingelse
break; // <!-- THIS IS IMPORTANT!

default:
// if nothing else matches
}


--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
Re: Switch question [message #184631 is a reply to message #184600] Mon, 13 January 2014 14:34 Go to previous message
Arno Welzel is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Thu Nov 21 23:04:13 GMT 2024

Total time taken to generate the page: 0.02658 seconds