dennismonsewicz Posted September 18, 2008 Share Posted September 18, 2008 Is there anyway to stack cases in PHP like you can in Javascript? IE: case "dog": case "cat": case "bird": DO SOMETHING Where the programming will say if its all three of those cases then do the statement at the end Quote Link to comment Share on other sites More sharing options...
.josh Posted September 18, 2008 Share Posted September 18, 2008 yes. Just don't put break; in it. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 18, 2008 Share Posted September 18, 2008 Yes that is possible, eg <?php $animal = isset($_GET['animal']) ? $_GET['animal'] : ''; switch($animal) { case 'dog': case 'cat': // etc echo 'You requested : ' $aniaml; break; default: echo 'Select an animal: <a href="test.php?animal=cat">Cat</a> | <a href="test.php?animal=dog">Dog</a>'; } You can also do: case 'dog': // more code here // PHP will execute all code here, plus the code in the next case case 'cat': // etc echo 'You requested : ' $aniaml; break; basically what is happening is PHP will execute all code within a switch/case until it comes across the break; keyword. Quote Link to comment Share on other sites More sharing options...
.josh Posted September 18, 2008 Share Posted September 18, 2008 To add to what wildteen said, it will execute the code if the condition evaluates true, for every condition. It just won't skip the other condition checks if you leave out the break. So in WT's examples, it won't execute the code if $animal != 'cat' Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 18, 2008 Share Posted September 18, 2008 To add to what wildteen said, it will execute the code if the condition evaluates true, for every condition. It just won't skip the other condition checks if you leave out the break. So in WT's examples, it won't execute the code if $animal != 'cat' You're sure about this? Because this <?php $animal = 'dog'; switch($animal) { case 'dog': case 'cat': // etc echo 'You requested : '. $animal; break; default: echo "none"; } ?> echoes 'You requested : dog' here Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted September 18, 2008 Author Share Posted September 18, 2008 yes wildteen's example works! <?php $animal = $_GET['animal']; switch($animal) { case 'dog': case 'cat': // etc echo 'You requested : '. $animal; break; default: echo "none"; } ?> I modified the $animal var to GET the animal from the URL, but it works like a charm! Thanks again! Quote Link to comment Share on other sites More sharing options...
.josh Posted September 18, 2008 Share Posted September 18, 2008 hmm...okay so it will not execute the script if the case evaluates false before the break, but it will after: <?php $animal = 'cat'; switch($animal) { case 'dog': echo "dog"; case 'cat': echo 'You requested : '. $animal; case 'pig' : echo "pig"; break; default: echo "none"; } ?> That will output You requested : catpig Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 18, 2008 Share Posted September 18, 2008 It'll run the code from where the condition is true (where the case matches), if animal was set to 'dog' then CV's code will return "dogcatpig" Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.