PHP switch
PHP Tutorial » PHP switch
What is a PHP switch?
The PHP switch statement is used to execute an instruction under many other conditions. It more or less works like PHP if-else-if.
Syntax switch Statement:
switch (n) {
case value1:
code to be executed if n=lvalue;
break;
case value2:
code to be executed if n=value2;
break;
case value3:
code to be executed if n=value3;
break;
... default:
code to be executed if n is different from all values;
}
?>
1. default is an optional statement.
2. Each case can have a break statement, used to finish sequence of statement.
3. break statement is optional to use in switch.
4. PHP allows you to use number, character, string, as well as functions in switch expression.
5. Nesting of switch statements is allowed
6. You can use semicolon (;) instead of colon (:).
PHP switch Statement
The default statement is used if no match is found.
<?php
$my_num=36;
switch($my_num){
case 12:
echo("My favorite number is equal to 12");
break;
case 24:
echo("My favorite number is equal to 24");
break;
case 36:
echo("My favorite number is equal to 36");
break;
default:
echo("My favorite number is not equal to 12, 24 or 36");
}
?>
Output: My favorite number is equal to 36
Related subjects:
PHP syntax
What is an array
PHP variabl
Array to comma separated
Tags: PHP switch, case, statement, default, case multiple conditions, multiple case, version, case or, greater than, instance of, vs match,
php switch/case null
What is a PHP switch?
Does PHP have switch?
What is the alternative to PHP switch?
Which is better if else or switch PHP?
PHP switch statement with String
PHP allows to pass string in switch expression.
<?php
$character = "Maths";
switch ($character)
{
case "Biology":
echo "Biology courses is 4 years.";
break;
case "Maths":
echo "Maths courses is 5 years.";
break;
case "Physics":
echo "Physics courses is 4 years.";
break;
case "Foreign languages":
echo "Foreign languages courses is 3 years.";
break;
default:
echo "No this is Wrong Choice.";
break;
}
?>
Output: Maths courses is 5 years.
PHP nested switch statement
Nested switch statement means switch statement inside another switch statement.
<?php
$car = "Hyundai";
$model = "Tucson";
switch( $car )
{
case "Honda":
switch( $model )
{
case "Amaze":
echo "Honda Amaze price is 5.93 - 9.79 Lakh.";
break;
case "City":
echo "Honda City price is 9.91 - 14.31 Lakh.";
break;
}
break;
case "Renault":
switch( $model )
{
case "Duster":
echo "Renault Duster price is 9.15 - 14.83 L.";
break;
case "Kwid":
echo "Renault Kwid price is 3.15 - 5.44 L.";
break;
}
break;
case "Hyundai":
switch( $model )
{
case "Creta":
echo "Hyundai Creta price is 11.42 - 18.73 L.";
break;
case "Tucson":
echo "Hyundai Tucson price is 22.39 - 32.07 L.";
break;
case "Xcent":
echo "Hyundai Xcent price is 6.5 - 10.05 L.";
break;
}
break;
}
?>
Output: Hyundai Tucson price is 22.39 - 32.07 L.
PHP switch, case, statement, default, case multiple conditions, multiple case, version, case or, greater than, instance of, vs match, php switch/case null
What is a PHP switch?
Does PHP have switch?
What is the alternative to PHP switch?
Which is better if else or switch PHP?
PHP switch Statement - php tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.