PHP 5 switch Statement

<?php $favcolor = "red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, nor green!"; } ?>

1 Response

Probably worth noting that for multiple case statements which have the same code executed you can waterfall down into them by stacking
case 'blue':
case 'red and blue':
echo 'It's red and blue and blue';
break;
default:
//// and so on

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.