Contact US

Get Your Project Done by our experts

PHP Switch Case

Switch case is a selectional statement which is used to perform different task depending on different conditions.

Syntax :
switch (user_input) {
    case name1:
       will be executed when user_input=name1;
    case name2:
       will be executed when user_input=name2;
    case name3:
       will be executed when user_input=name3;
       will be executed if user_input does not match any of the case names;

Explaination : The user gives an input which we have stored in a variable named user_input. The value of user_input will be matched with each case name to check if it has a match. Whichever case name matches, that block of code will be executed.If the user_input doesn't match with any of the case names then the default case will be executed.

Here is an example of Switch case 



$user_input = "morning";

switch ($user_input) {
    case "morning":
       echo "time for breakfast";
    case "afternoon":
        echo "time for lunch";
    case "night":
        echo "time for dinner";
        echo "Input does not match any case";


The Output for the above code :

time for breakfast