Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #12667

Re: switch says value is equal to case when it is not

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.php
Subject Re: switch says value is equal to case when it is not
Date 2014-01-14 22:15 +0000
Organization A noiseless patient Spider
Message-ID <lb4ctq$adc$5@dont-email.me> (permalink)
References <x76wm4qeiagq.vnb3xbwh1l0o$.dlg@40tude.net>

Show all headers | View raw


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

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web