php switch statement

The PHP switch case works exactly as the If Else statement does. The switch statement is applied to perform more than one operation according to different conditions.

Related: 7 easiest example of PHP explode and PHP implode

By using the switch keyword, you set your expression or original value, and after that, you have to implement your different conditions that are applied with the case keyword.

If your given condition matches your expression, then your program executes the statement that you have applied. But take care that if any of your conditions do not match, then your default value will be executed.

Switch Case Syntax

After seeing the Syntax of the Switch case, you will know how to execute a statement according to the given condition.

switch (expression or original value)
{
	case 1:
		the statement will execute if case 1 matched with expression;
		break;
	case 2:
		the statement will execute if case 2 matched with expression;
		break;
	case 3:
		the statement will execute if case 3 matched with expression;
		break;
	default;
		If no condition matches, then the default value will continue;
}

PHP Switch Case

We have some great examples of PHP switch statement available to explain you better, after seeing which you will know how the switch statement works.

<?php
   
$salary = "2000";

switch ($salary) {
    case "6000":
        echo "My salary is 6000";
        break;
    case "3000":
        echo "My salary is 3000";
        break;
    case "2000":
        echo "My salary is 2000";
        break;
    default:
        echo "Salary Delay";
}
   
?>

Output:- My salary is 2000

PHP Switch Case Default

As we mentioned above, if none of your conditions match, then your default value will continue. From the example below, you will know how the Switch Case Default works.

<?php
   
$salary = "2000";

switch ($salary) {
    case "5000":
        echo "My salary is 5000";
        break;
    case "8000":
        echo "My salary is 8000";
        break;
    case "4000":
        echo "My salary is 4000";
        break;
    default:
        echo "Salary Delay";
}
?>

Output:- Salary Delay

PHP Switch Case Example

<?php

switch ('shoes') {
    case "shirt":
        echo "I have a shirt";
        break;
    case "tie":
        echo "I need a tie";
        break;
    case "shoes":
        echo "This is my shoes";
        break;
    default:
        echo "I have nothing";
}
?>

Output:- This is my shoes

Read Also: 25 important examples of SQL where clause

In the following example, we have used the date function which tells us the name of the current day. We have displayed the message according to days.

<?php

$day = date("D");

switch ($day) {

	case 'Mon':
		echo 'Monday';
		break;

	case 'Tue':
	    echo 'Tuesday';
		break;

	case 'Wed':
	    echo 'Wednesday';
		break;

	case 'Thu':
		echo 'Thursday';
		break;

	case 'Fri':
		echo 'Friday';
		break;

	default:
		echo 'Holiday';
		break;
}

?>

Output:- Your current day will be display

PHP Switch Multiple Cases

If you want to run your statement inside Multiple Cases, then for that you have to combine Multiple Cases together.

<?php

$salary = "5000";

switch ($salary) {
    
    case "5000":
    case "8000":
        echo "My salary is 5000 + 8000";
        break;
        
    case "4000":
        echo "My salary is 4000";
        break;
        
    default:
        echo "Salary Delay";
}

?>

Output:- My salary is 5000 + 8000

Read Also: 9 Easiest examples of SQL max function

In the following example, we used greater-than (>) and less than (<) operators and detected that the age is greater than 40 or less than 40.

<?php

$age = "50";

switch ($age) {
    
    case ($age > 40):
        echo "Age is 50";
        break;
        
    case ($age < 40):
        echo "Age is less then 40";
        break;
        
    default:
        echo "Not found";
}

?>

Output:- Age is 50

PHP Switch vs If

The switch statement works the same way as If Else Condition does.

If Statement

<?php
$salary = "5000";

if($salary){
    echo "My salary is ". $salary;
}

Output:- My salary is 5000



If Else Statement

$salary = "5000";

if($salary>5100){
    echo "My salary is ". $salary;
}else{
    echo "Nothing";
}

Output:- Nothing



If Else If Statement

$salary = "5000";

if($salary>5100){
    echo "My salary is ". $salary;
}else if ($salary<5100){
    echo "My salary is ". $salary;
}else{
    echo "Nothing";
}

?>

Output:- My salary is 5000

Read Also: 13 simple examples of SQL sum and SQL count

Note: All the examples you have seen inside this article have all been made by us and tested thoroughly. So you can use all these without any difficulty.

Read Also: 20 best examples of SQL like operator