PHP Learn It! PHP Switch Statement
"Best Deal"

"Bored - Do You Agree?"
t-shirtgirlspics

Bored - Studied Hard - Enjoy Yourself "TAKE A BREAK"

"Decide Now-So Many Choices"
"Amazing Deals"
Once You Know, You Newegg

PHP Switch Statement

Welcome! Switch statements are just like if..else conditional statements where a block of code is executed if the condition is true. Lets look at the syntax of a switch statement...

Learn PHP and MySQL fast!

Learn how you can start building rich interactive database driven sites like a professional developer easily and quickly...

>> Order your copy now for just $39.95 <<

Switch Statement Syntax

switch (expression)
{
	case case_1:
		//execute first block of code
		break;
	case case_2:
		//execute second block of code
		break;
	case case_3:
		//execute third block of code
		break;
	default:
		//execute this block of code if expression doesn't match any case

}	

PHP Switch Statement

Switch statement should be used in the code when you want to evaluate different cases of a given scenario. The switch statement takes a single variable as input and then checks it against all cases we have in our switch statement.

Let's imagine you own a computer retail store. Depending on the product name we want to display the price of that product.

Now, instead writing separate if else statements for each product name, we simple use the PHP switch statement on the product name variable to evaluate which product we want to the display the price for. Let's have look at the example.

Example - PHP Switch Statement

<html>
<body>

<?php
//set product name
$product_name = "Processors";

switch ($product_name)
{
	case "Video Cards":
		echo "Video cards range from $50 to $500";
		break;
	case "Monitors":
		echo "LCD monitors range from $200 to $400";
		break;
	case "Processors":
		echo "Intel processors range from $100 to $1000";
		break;
	default:
		echo "Sorry, we don't carry $product_name in our catalog";
		break;
}
?>

</body>
</html>

The above switch statements evaluates the product name and displays the price range for that product. Since, we set the $product_name to "Processor", the above code outputs "Intel processors range from $100 to $1000".

PHP Switch Statement: The default case

What if someone asks for a product we don't carry? In that case, the default statement in switch code will be executed. In the example below we set the product name to "Undewear".

Example- The Default Case in PHP Switch Statement

<html>
<body>

<?php
//set product name
$product_name = "Undewear";

switch ($product_name)
{
	case "Video Cards":
		echo "Video cards range from $50 to $500";
		break;
	case "Monitors":
		echo "LCD monitors range from $200 to $400";
		break;
	case "Processors":
		echo "Intel processors range from $100 to $1000";
		break;
	default:
		echo "Sorry, we don't carry $product_name in our cateloge";
		break;
}
?>

</body>
</html>

The code will executed the default code block since we don't carry underwear is not part of our switch statement cases. The code will output "Sorry, we don't carry Undewear in our catalog".

Tip: It is always good to include the default case in your switch statements. It will help you debug your code better during testing. In case, none of your switch cases match in your switch statement during code execution, the default case will be executed.

PHP Switch Statement: The Break Statement

The break statement in PHP switch code prevents the code from executing other cases in the switch statement. When a case is matched in a switch statement, the break statement basically stops the code execution. If the break statement is not there, then each case is executed. Consider the following example with two same product cases.

Example- The Break Statement in PHP Switch Statement

<html>
<body>

<?php
//set product name
$product_name = "Processors";

switch ($product_name)
{
	case "Video Cards":
		echo "Video cards range from $50 to $500";
	
	case "Monitors":
		echo "LCD monitors range from $200 to $400";
	
	case "Processors":
		echo "Intel processors range from $100 to $1000";
	
	case "Processors":
		echo "AMD processors range from $100 to $1000";
}
?>

</body>
</html>

As we can see in the above example, we don't have the break statement after each switch case and we have two cases for the product "Processors". So, the above code will evaluate both cases for "Processors".

The code evaluates the first case and prints "Intel processors range from $100 to $1000" and then it goes to the next case and prints "AMD processors range from $100 to $1000".

Tip: If you're a new to switch statements, it is good to use the break statement in your switch cases to prevent any confusion.

From HTML to PHP programming...
Learn how to build websites in PHP

<< PHP If..else Top PHP Arrays >>