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