You are on page 1of 2

SIMPLE PHP TUTORIAL 3 by pox Switch statement.

This is the alternative of the if-else statement and it creates more options or handling complex conditions lather than using the nested if statements that can make the job somehow tedious in case you have many options. Lets take a look on the following example bellow:-

<?php $num=10; switch ($num){ case 1: echo "the number is one"; break; case 4: echo "the number is four"; break; case 5: echo "the number is five"; break; case 6: echo "the number is six"; break; // if all the above cases are untrue then the default will be executed default: echo "the number is not one, four, five or six"; } ?>

From the above example we have our number that is 10 which we declared as $num, then case 1 it means that we ask if the declared number is one, case 4 also it means if the number is four likewise to five and six. And if the above numbers in each case is not equals to the declared (10), then the default case will be executed. But if you put 10 in one of the case then that particular case will be executed instead of the default. Simply if the case is true then the statement below it will be executed and displayed.

You might also like