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
Think its something like this $foo = is_numeric("45"); switch($foo) { case false: code here break; case true: code here break; } Code (markup):
switch(true) and switch(false) can be used depending on what is trying to be accomplished. What exactly are you trying to do?
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.
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.
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.
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.