AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP switch


<< Previous Page
PHP if else, elseif
Next Page >>
PHP foreach

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:

<?php
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.

Example (this is HTML - PHP editor, change text, values on this window)
<?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.

Example (this is HTML - PHP editor, change value of $numbr then click Execute)
<?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.

Example (this is HTML - change value of $numbr then click Execute)
<?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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.