Hi guys, in this tutorial we will learn PHP Switch Statement.
witch cases are control structures that allow you to execute code segments based on the given conditions. This creates dynamic PHP behavior.
PHP provides a switch statement that allows you to conditionally execute the code block. The PHP switch statement compares an expression against a variable or many different values and executes a code block based on its same value.
Syntax:
switch(n){
case label1:
//===========
//label1 Code
//===========
break;
case label2:
//===========
//label2 Code
//===========
break;
case label3:
//===========
//label3 Code
//===========
break;
...
default:
//===========
//default Code
//===========
}
case label1:
//===========
//label1 Code
//===========
break;
case label2:
//===========
//label2 Code
//===========
break;
case label3:
//===========
//label3 Code
//===========
break;
...
default:
//===========
//default Code
//===========
}
Let’s take a look at the following example.
<?php
$theme = 2;
switch ($favcolor) {
case 1:
echo "Theme 1 is a seleced";
break;
case 2:
echo "Theme 2 is a seleced";
break;
case 3:
echo "Theme 3 is a seleced";
break;
default:
echo " Default theme seleced!";
}
?>
Output:
Theme 2 is a seleced
I hope this tutorial help for you.
Related Post