switch statement

Discussion in 'PHP' started by aayybb, Dec 1, 2008.

  1. #1
    Can somebody explain the following to me? I thought we need variable for switch statement like switch($variable). I don't know how (false) and (true) work here.


    $foo = "not a number";
    switch(false)
    {
    case "1": { $foo = "1"; break; }
    case "2": { $foo = "2"; break; }
    default: { $foo = "0"; }
    }

    echo 'false '.$foo; // will produce 0

    $foo = "not a number";
    switch(true)
    {
    case "3": { $foo = "3"; break; }
    case "2": { $foo = "2"; break; }
    default: { $foo = "0"; }
    }

    echo 'true '.$foo; // will produce 3
     
    aayybb, Dec 1, 2008 IP
  2. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Think its something like this
    
    $foo = is_numeric("45");
    switch($foo)
    {
    case false:
    code here
    break;
    
    case true:
    code here
    break;
    
    }
    
    Code (markup):
     
    atlantaazfinest, Dec 1, 2008 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    switch(true) and switch(false) can be used depending on what is trying to be accomplished.

    What exactly are you trying to do?
     
    jestep, Dec 1, 2008 IP
  4. aayybb

    aayybb Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I got this example from PHP.net and I don't understand and it produced results different from the post.
    Well I still don't understand the code I posted and how false and true work here.
     
    aayybb, Dec 1, 2008 IP
  5. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #5
    you can go to this page
    http://id2.php.net/manual/en/control-structures.switch.php
    Code (markup):
     
    misbah, Dec 1, 2008 IP
  6. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The example posted on php.net is incorrect, I had a look at it and tested it, but it's bogus.

    about your examples:
    If you test switch(true) that compares the statement per case on matching true.
    If a clause can be equated to logical false, the switch function skips and tests the next
    case. switch() picks the first clause that matches the value you test on (logical true),
    if there is no such case it picks the default: case, and if you don't specify a default: case, it has no result.

    With the switch() function, any single variable or string in itself as case is 'true'.

    If I test "1", that cannot be asserted as false :

    $foo = "not a number";
    switch(false)
    {
    case "1": { $foo = "1"; break; }
    case "2": { $foo = "2"; break; }
    default: { $foo = "0"; }
    }

    "1" is true,
    "2" is true,
    As there is no case that equates to 'false',
    but there is a default: case,
    switch() defaults to that and sets $foo to "0".

    in the second example,
    switch(true)
    {
    case "3": { $foo = "3"; break; }
    case "2": { $foo = "2"; break; }
    default: { $foo = "0"; }
    }

    "3" cannot be asserted as false, hence it's true. The switch statement picks the first case that matches true and does not look any further, so that's your first match.

    There is a 'but' : the php interpreter assumes false=0, true=1, that is the main pitfall in using switch(true/false) with numbers.

    switch(true)
    {
    case "0": { $foo = "3"; break; }
    case "2": { $foo = "2"; break; }
    default: { $foo = "0"; }
    }

    As 0 or "0" can logically be equated to false, switch() will pick case "2" as first match.
     
    juust, Dec 2, 2008 IP
  7. aayybb

    aayybb Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thank you so much for the explanation. Now I understand. I knew the code at PHP.net was wrong since I tested it out but I didn't know why. Thanks again for teaching me.
     
    aayybb, Dec 3, 2008 IP
  8. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #8
    Maybe this example will make it easier to understand.

    <?php
    
    class a {}
    class b {}
    class c {}
    
    $var = new b;
    
    switch (true)
    {
    	case $var instanceof a:
    		echo 'Is an A';
    	break;
    	case $var instanceof b:
    		echo 'Is a B';
    	break;
    	case $var instanceof c:
    		echo 'Is a C';
    	break;
    	default:
    		echo 'Do not know what it is';
    	break;
    }
    
    ?>
    PHP:
    Basically when using a boolean value for switch(value), you're inverting the execution so that there is a constant value being evaluated against multiple expressions, instead of one expression being evaluated againts multiple values.
     
    joebert, Dec 3, 2008 IP