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
Return to the default flat view Create a new topic Submit Reply
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 previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
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: Thu Nov 28 02:52:15 GMT 2024

Total time taken to generate the page: 0.04348 seconds