Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #12663 > unrolled thread
| Started by | richard <noreply@example.com> |
|---|---|
| First post | 2014-01-14 14:54 -0500 |
| Last post | 2014-01-15 18:06 +0000 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.php
switch says value is equal to case when it is not richard <noreply@example.com> - 2014-01-14 14:54 -0500
Re: switch says value is equal to case when it is not "Beauregard T. Shagnasty" <a.nony.mous@example.invalid> - 2014-01-14 20:42 +0000
Re: switch says value is equal to case when it is not Doug Miller <doug_at_milmac_dot_com@example.com> - 2014-01-14 21:09 +0000
Re: switch says value is equal to case when it is not Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-14 22:15 +0000
Re: switch says value is equal to case when it is not Frank Catry <frank.catry@gmail.com> - 2014-01-15 09:32 -0800
Re: switch says value is equal to case when it is not Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-15 18:06 +0000
| From | richard <noreply@example.com> |
|---|---|
| Date | 2014-01-14 14:54 -0500 |
| Subject | switch says value is equal to case when it is not |
| Message-ID | <x76wm4qeiagq.vnb3xbwh1l0o$.dlg@40tude.net> |
<?php
$go=$_GET['a'];
if(empty($go)){$go="home";}
if ($go==2){include "http://mroldies.net/radio/24hours.php";}
if ($go==1){include"http://mroldies.net/test/index2.php";}
if ($go=="home"){include "http://mroldies.net/home1.php";}
switch ($go) {
case 60 || 61 || 62 || 63 || 64 || 65 || 66 || 67 || 68 || 69:
include "http://mroldies.net/songs/19".$go.".html";
case "A" || "B" || "C" || "D" || "E" || "F" || "G" || "H" || "I" || "J" ||
"K" || "L":
include "http://mroldies.net/songs/".$go.".html";
case "M" || "N" || "O" || "P" || "Q" || "R" || "S" || "T" || "U" || "V" ||
"W" || "X" || "Y" || "Z":
include "http://mroldies.net/songs/".$go.".html";
}
?>
"home" is matched in every case.
Why?
"Case" is not conditional.
There must be an EXACT match or false is returned.
[toc] | [next] | [standalone]
| From | "Beauregard T. Shagnasty" <a.nony.mous@example.invalid> |
|---|---|
| Date | 2014-01-14 20:42 +0000 |
| Message-ID | <lb47fr$fe8$1@dont-email.me> |
| In reply to | #12663 |
richard the sto0pid wrote: > Why? Perhaps if you wrote some valid code once in a while, it might actually work. -- -bts -This space for rent, but the price is high
[toc] | [prev] | [next] | [standalone]
| From | Doug Miller <doug_at_milmac_dot_com@example.com> |
|---|---|
| Date | 2014-01-14 21:09 +0000 |
| Message-ID | <XnsA2B5A44A18F0Edougmilmaccom@78.46.70.116> |
| In reply to | #12663 |
richard <noreply@example.com> wrote in news:x76wm4qeiagq.vnb3xbwh1l0o$.dlg@ 40tude.net: > case 60 || 61 || 62 || 63 || 64 || 65 || 66 || 67 || 68 || 69: For at least the third time now, RtS: This does NOT work the way you think it does. It does *not* test to see if the value of the switch variable is equal to 60 or 61 or ... or 69. Instead, it compares the value of the switch variable to the bitwise logical conjunction 60 || 61 || 62 || ... || 69 which necessarily is a true value -- thus any non-zero value of the switch variable will match. The same issue naturally affects the other two 'case' statements which you have also coded incorrectly. It would also behoove you to read about, and understand, the use of the 'break' command inside 'switch'.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-01-14 22:15 +0000 |
| Message-ID | <lb4ctq$adc$5@dont-email.me> |
| In reply to | #12663 |
On Tue, 14 Jan 2014 14:54:40 -0500, richard wrote:
> [my case statements are broken]
Because you have fucked up the case statements.
Irrespective of what you may have deduced from the limited testing that
you have carried out, combined with your mis-comprehension of the manual
and your invalid attempts to move paradigms from other languages into php,
the effect of using || to combine multiple items in the case test is not
doing what you expect.
x || y [ || ... ] will either render TRUE or FALSE
case x || y || z:
is the same as either:
case TRUE:
or
case FALSE:
depending on whether your x || y || z evaluates as TRUE or FALSE.
switch ( a ) {
case x || y || z: echo "bing"
}
will output the text "bing" if the truthness of a matches the truthness
of x || y || z
Analysing your code more specifically, if any of the strings in your
multiple string case x || y || z: evaluates as TRUE (any non empty string
other than a string representation of numeric 0 evaluates true) then you
are testing the switch variable for TRUE, and if the switch variable also
evaluates as truthy, then the case statement is matched.
so - to rewrite your code in a way that shows what is actually happening:
<?php
switch (true) {
case true:
include "http://mroldies.net/songs/19".$go.".html";
case true:
include "http://mroldies.net/songs/".$go.".html";
case true:
include "http://mroldies.net/songs/".$go.".html";
}
?>
Note also that because you have no break statements, every executable
statement after the first matching case is executed.
You could in fact replace your switch block with the following 3 php
statements to obtain the same effect:
include "http://mroldies.net/songs/19".$go.".html";
include "http://mroldies.net/songs/".$go.".html";
include "http://mroldies.net/songs/".$go.".html";
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Frank Catry <frank.catry@gmail.com> |
|---|---|
| Date | 2014-01-15 09:32 -0800 |
| Message-ID | <c905b5ea-ea60-4ad1-ba81-235dd7e7fbf6@googlegroups.com> |
| In reply to | #12663 |
between every CASE you need BREAK;
Op dinsdag 14 januari 2014 20:54:40 UTC+1 schreef richard:
> <?php
>
>
>
> $go=$_GET['a'];
>
> if(empty($go)){$go="home";}
>
>
>
> if ($go==2){include "http://mroldies.net/radio/24hours.php";}
>
> if ($go==1){include"http://mroldies.net/test/index2.php";}
>
> if ($go=="home"){include "http://mroldies.net/home1.php";}
>
>
>
> switch ($go) {
>
>
>
> case 60 || 61 || 62 || 63 || 64 || 65 || 66 || 67 || 68 || 69:
>
> include "http://mroldies.net/songs/19".$go.".html";
>
>
>
> case "A" || "B" || "C" || "D" || "E" || "F" || "G" || "H" || "I" || "J" ||
>
> "K" || "L":
>
> include "http://mroldies.net/songs/".$go.".html";
>
>
>
> case "M" || "N" || "O" || "P" || "Q" || "R" || "S" || "T" || "U" || "V" ||
>
> "W" || "X" || "Y" || "Z":
>
> include "http://mroldies.net/songs/".$go.".html";
>
>
>
> }
>
>
>
>
>
> ?>
>
>
>
> "home" is matched in every case.
>
> Why?
>
>
>
> "Case" is not conditional.
>
> There must be an EXACT match or false is returned.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-01-15 18:06 +0000 |
| Message-ID | <lb6in8$50a$3@dont-email.me> |
| In reply to | #12678 |
On Wed, 15 Jan 2014 09:32:47 -0800, Frank Catry wrote: > between every CASE you need BREAK; Actually you don't, and in this case he probably doesn't. If he wants to use a switch case to execute a single statement on any of multiple matches, he needs to use the appropriate combination of case and break statements, as described and demonstrated at http://www.php.net/manual/en/control-structures.switch.php His biggest problem at the moment is that he doesn't understand that the comparison that's being made is not the comparison that he thinks is being made. A secondary problem that he has had for a long time is that occasionally he picks up on a piece of incorrect crap that he reads on the internet such as "between every CASE you need BREAK;", latches on to it, and then remains convinced that it is gospel writ by the almighty himself even when he is told quite clearly that it is wrong. -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web