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

Home » Imported messages » comp.lang.php » switch says value is equal to case when it is not
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
switch says value is equal to case when it is not [message #184666] Tue, 14 January 2014 19:54 Go to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
<?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.
Re: switch says value is equal to case when it is not [message #184667 is a reply to message #184666] Tue, 14 January 2014 20:42 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:

> 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
Re: switch says value is equal to case when it is not [message #184668 is a reply to message #184666] Tue, 14 January 2014 21:09 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: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'.
Re: switch says value is equal to case when it is not [message #184670 is a reply to message #184666] Tue, 14 January 2014 22: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 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(at)gmail(dot)com
Re: switch says value is equal to case when it is not [message #184682 is a reply to message #184666] Wed, 15 January 2014 17:32 Go to previous messageGo to next message
frank.catry is currently offline  frank.catry
Messages: 9
Registered: October 2013
Karma: 0
Junior Member
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.
Re: switch says value is equal to case when it is not [message #184683 is a reply to message #184682] Wed, 15 January 2014 18:06 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
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(at)gmail(dot)com
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: php.ini loading
Next Topic: How to locally install Wordpress, Ubuntu 8.10 , XAMPP already installed.
Goto Forum:
  

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

Current Time: Fri Nov 22 15:45:12 GMT 2024

Total time taken to generate the page: 0.02421 seconds