PHP Switch Case Conditional Statement Syntax and Code Example

PHP Switch Case Statement

PHP Switch Case is also a conditional statement. You also say that it is the alternative solution and easy syntax of multiple if statement. It is used for multiple test condition.

Switch Case Syntax

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
	
		switch(variable)
		{
			case "label1":
			{
				code is execute when value of veriable is 1;
			}
			break;
			case "label2":
			{
				code is execute when value of veriable is 2;
			}
			break;
			case "label3":
			{
				code is execute when value of veriable is 3;
			}
			break;
			case "label4":
			{
				code is execute when value of veriable is 4;
			}
			break;
			default :
			{
				code is execute when value of veriable is 1;
			}
		}
	?>
  </body>
</html>

Example 1 – Switch Case Statement Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$x = 3;
		
		switch($x)
		{
			case "1":
			{
				echo "You are good in Science";
			}
			break;
			case "2":
			{
				echo "You are good in Hindi";
			}
			break;
			case "3":
			{
				echo "You are good in Maths";
			}
			break;
			case "4":
			{
				echo "You are good in English";
			}
			break;
			default :
			{
				echo "You are good in all subjects";
			}
		}
	?>
  </body>
</html>

In the above code, we describe how you can use switch case conditional statement.

Here we declare a variable $x and we declare code and which value of variable which code will be run and get the result.

Here we declare the value of variable $x is 3 than the case “3” code part show on the web page “You are good in Maths”

PHP Switch Case
Fig.1 – PHP Switch Case.

How Default Used in Switch Case

The Default case code part is executed when any of the case not matches.

Example 2 – Switch Case Statement Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$x = white;
		
		switch($x)
		{
			case "green":
			{
				echo "The color of the fertility, growth and auspiciousness.";
			}
			break;
			case "orange":
			{
				echo "The color of strength and courage.";
			}
			break;
			case "blue":
			{
				echo "The color of the most truth of the universe.";
			}
			break;
			break;
			default :
			{
				echo "The color of peace,purity and honesty.";
			}
		}
	?>
  </body>
</html>

In the above code, we describe how you can use switch case conditional statement.

Here we declare a variable $x and the value of variable will not have any matches in switch case condition.

PHP Switch Statement Example
Fig.2 – PHP Switch Statement.
You can leave a response, or trackback from your own site.

2 Responses to “PHP Switch Case Conditional Statement Syntax and Code Example”

  1. BrettHuzaifa says:

    Hi there, just became alert to your blog through Google, and found that it’s truly informative. I am going to watch out for brussels. I’ll be grateful if you continue this in future. Many people will be benefited from your writing. Cheers!|

  2. Aw, this was an extremely nice post. Finding the time and actual effort to create a very good article… but what can I say… I put things off a whole lot and don’t manage to get anything done.

Leave a Reply to the article


Learn PHP

Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape