Switch statement is used to select one of the many blocks code to be executed.
You can use the “switch” statement to alter the flow of a program.
Conditional statements are used to perform actions on different conditions.
Learn PHP - PHP tutorial - Switch Statement - PHP examples - PHP programs
php scripts Syntax :
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
click below button to copy the code. php tutorial - team Copy Code
Sample Code in php : sample php program
<!DOCTYPE html>
<html >
<body>
<?php
$fruit = "mango";
switch ($fruit)
{
case "apple":
echo "My favorite fruit is apple";
break;
case "mango":
echo "My favorite fruit is mango";
break;
case "guava":
echo "My favorite fruit is guava";
break;
default:
echo "Failure..!!!";
}
?>
</body>
</html>
click below button to copy the code. php tutorial - team Copy Code
php programmer Code Explanation :
In PHP, $fruit = "mango"; specifies the variable $fruit = mango variable holding the variable = mango.
Here, switch ($fruit) will call the variable “$fruit” in the code, which we have specified as “mango”.
We have the switch-cases as case “apple”, case “mango” and case “guava” which executes based on the function call for that variable ($fruit). If these cases fails it will execute the default case statement.
break; is the statement that executes a line break in the code execution.
php development Sample Output :
Here the PHP statement executes the switch condition by printing the echo statement “My favorite fruit is mango”.
Related Searches to php switch statement
php switch multiple case
switch case in php with multiple case example
php switch case greater than
php switch case range
php switch case multiple variables
php switch return
php switch multiple cases same
php 7 switch
php switch statement multiple case
PHP tutorials
how to become a php developer
online php classes
online php course
online php courses
php sql query
php tutorial for beginners pdf
php web development
php website project php tutorial for beginners tutorial php php website learn php online php programs php programming language php online course php mysql tutorial php example php training php source code php 7 tutorial tutorialspoint php php url script php advanced php tutorial php web development php javascript php ajax tutorial php lernen php code generator php for dummies php developer online php classes online php course php developer course php development tutorials php tutorial for beginners pdf php programming language php server script php website project phpmyadmin sql programing php php programming courses php training course php tutorial classes php tutorial for beginners pdf tutorial php class php tutorial for beginners with examples pdf php tutorial point php interview questions php pdf php basics php syntax php tutorial for beginners php tutorial pdf sphp tutorial