Home »
Imported messages »
comp.lang.php »
switch says value is equal to case when it is not
|
|
|
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   |
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 #184683 is a reply to message #184682] |
Wed, 15 January 2014 18:06  |
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
|
|
|
Goto Forum:
Current Time: Fri Apr 11 04:02:30 GMT 2025
Total time taken to generate the page: 0.06388 seconds