PHP Switch Case Tutorial

– The PHP switch statement is used to execute distinct action based on different conditions.

– PHP switch statement is mainly used to perform one statement from different different conditions.

– It works like PHP If-else-if function. It is also used as alternative of if-else-if statement in PHP.

– The switch case statement checks a series of values until it finds a match which fulfill the condition, and then perform the Block of code according to that match.

– The switch case default allows us to make a decision from the different choices.

– Switch statement is also similar to the series of IF statement in PHP.

– The Switch case statements are a alternative for long IF statements that compare a variable to some integral values.

– The condition following the keyword switch can be a variable or any other expression like a string, an integer, a character.

– Every constant in all cases must be different from each other.

– The switch statement is a control statement that authorize a value to change control of execution.

– When we execute the program having switch statement at first the expression following the keyword is switch is check.

– All the case statements check one by one for the value of given match. When a match is found the program execute the expression following that case.

– If no match is found with any of case statements, then only the default case statement is executed.

SYNTAX :

switch (n) {
    case case1:
        code to be executed if n=case1;
        break;
    case case2:
        code to be executed if n=case2;
        break;
    case case3:
        code to be executed if n=case3;
        break;
    ...
    default:
        code to be executed if n is different from all labels;
}

This is works as : At the beginning we have a single expression n that is analyze once.

The value of the expression is compared with the values for the given each case into structure.

If there is a match, the related block of code with that case is executed.

Break is used for running into next case automatically. If there is no match then the default statement is executed.

Important points about switch case statement :

1. Duplicate case values are not allowed.

2. The default statement is not compulsory it is optional.

3. There is a only one default statement in switch case. More than one may cause a fatal error.

4. Break statement is also optional.

5. The break statement is used inside the switch statement. It is used to terminate statement. When break statement reached,it will terminate the switch and the flow of control jumps to next statement.

6. Nested switch statements are allowed. It means we used switch statement within another switch statement. It makes program more complex.

7. We can use semicolon (;) instead of colon (:). It will not generate any error.

FLOWCHART

php switch

EXAMPLE

<!DOCTYPE html>
<html>
<body>

<?php
$language = "PHP";

switch ($language) {
    case "JAVA":
        echo "Your favorite language is JAVA!";
        break;
    case "PHP":
        echo "Your favorite language is PHP!";
        break;
    case "Python":
        echo "Your favorite language is Python!";
        break;
    default:
        echo "Your favorite language is neither JAVA, PHP, nor Python!";
}
?>
 
</body>
</html>

Below is the output of the above code:

Your favorite language is PHP!

PHP SWITCH STATEMENT IS FALL-THROUGH

– PHP switch statement is a fall-through.

– It means if there is no break statement then it will execute all statement after getting first match.

EXAMPLE :

<!DOCTYPE html>
<html>
<body>

<?php
$language = "PHP";

switch ($language) {
    case "JAVA":
        echo "Your favorite language is JAVA!";
        echo "</br>";
        break;
    case "PHP":
        echo "Your favorite language is PHP!";
        echo "</br>";
    case "Python":
        echo "Your favorite language is Python!";
        echo "</br>";
    default:
        echo "Your favorite language is neither JAVA, PHP, nor Python!";
}
?>
 
</body>
</html>

Below is the output of the above code:

Your favorite language is PHP!
Your favorite language is Python!
Your favorite language is neither JAVA, PHP, nor Python!

NESTED SWITCH STATEMENT

– PHP supports nested switch statement it means we defined another switch statement within switch statement.

– This nested switch statement create complex code so sometimes it create error.

– Take a look of nested switch statement.

Example

<!DOCTYPE html>
<html>
<body>

<?php
$semester = "2";                   
        $language = "PHP";    
        switch( $semester )    
        {    
            case "1":    
                switch( $language )     
                {    
                    case "C":    
                           echo "You learn C language in semester 1";   
                        break;    
                    case "C++":    
                           echo "You learn C++ language in semester 1";    
                        break;     
                }    
                break;    
            case "2":    
                switch( $language )     
                {    
                    case "PHP":    
                        echo "You learn PHP language in semester 2";  
                        break;    
                    case "JAVA":    
                           echo "You learn JAVA language in semester 2";  
                        break;    
                }    
                break;    
            case "3":    
                switch( $language )     
                {    
                    case "Android":    
                        echo "You learn Android language in semester 3";  
                        break;      
                    case ".net":    
                           echo "You learn .net language in semester 3";  
                        break;    
                }    
                break; 
        } 
        
?>
 
</body>
</html>

– Output of the nested switch statement as below :

You learn PHP language in semester 2