Saturday, November 16, 2024
Google search engine
HomeLanguagesSwitch Statement in PHP | Decision-Making Statements

Switch Statement in PHP | Decision-Making Statements

Switch Statement in PHP | (Decision-Making Statements). Here we will learn what is switch case or decision making statement in PHP and how to use switch case statement in PHP.

Switch Statement in PHP | (Decision-Making Statements)

Basically the switch statement is also known as a decision statement in PHP and other programming languages. The switch-case statement tests a variable against a range of values ​​until it finds a match, and then executes a block of code corresponding to that match.

Note:- The switch-case statement is an alternative to the if-elseif-else statement.

Syntax – Switch Statement

 Switch (expression){
	 
	case 1:
	 
	//Code to execute here
	 
	break;
	 
	case 2:
	 
	//Code for case 2
	 
	break;
	 
	default:
	 
	//The default case code here
	 
	//This will execute if none of the case is true
	 
 }


The flow chart – Switch case statement

Switch Case Statement

Example 1 – Switch Case Statement

This is a basic example of using a PHP switch with 5 cases and one default case. We declared only one variable and gave it a value. The variable is used as an expression in the switch statement.

<?php 

$number = 5;
switch ($number) {

    case 1:
        echo "The executed case number is: 1";
        break;
    case 2:
        echo "The executed case number is: 2";
        break;
    case 3:
        echo "The executed case number is: 3";
        break;
    case 4:
        echo "The executed case number is: 4";
        break;
    case 5:
        echo "The executed case number is: 5";
        break;
    default:
        echo "The default case";
        break;
}

?>

Source Code – Switch case Statement
<!DOCTYPE html>
<html>
<title>Switch Statement | (Decision-Making Statements) | PHP Tutorial</title>
<head></head>
<body>

<h4>Switch Statement | (Decision-Making Statements)</h4>


<?php 

 $number = 5;

 switch ($number) {

    case 1:
        echo "The executed case number is: 1";
        break;
    case 2:
        echo "The executed case number is: 2";
        break;
    case 3:
        echo "The executed case number is: 3";
        break;
    case 4:
        echo "The executed case number is: 4";
        break;
    case 5:
        echo "The executed case number is: 5";
        break;
    default:
        echo "The default case";
        break;
 }

?>

</body>
</html>

Example Second – switch case in PHP with Day

Let’s take a new example of switch case statement with day, we have used the date function to get the day number of the current week. The day number can be taken by using the “n” date formatting character in date function. See the code of switch case statement with the day.

<?php
$today = date("D");
switch($today){
    case "Mon":
        echo "Today is Monday. Clean your house.";
        break;
    case "Tue":
        echo "Today is Tuesday. Buy some food.";
        break;
    case "Wed":
        echo "Today is Wednesday. Visit a doctor.";
        break;
    case "Thu":
        echo "Today is Thursday. Repair your car.";
        break;
    case "Fri":
        echo "Today is Friday. Party tonight.";
        break;
    case "Sat":
        echo "Today is Saturday. Its movie time.";
        break;
    case "Sun":
        echo "Today is Sunday. Do some rest.";
        break;
    default:
        echo "No information available for that day.";
        break;
}
?>

Source Code – switch case in PHP with Day
<!DOCTYPE html>
<html lang="en">
<head>
    <title>PHP switch-case Statement With Day</title>
</head>
<body>
<h4>PHP switch-case Statement With Day</h4>

<?php
$today = date("D");
switch($today){
    case "Mon":
        echo "Today is Monday. Clean your house.";
        break;
    case "Tue":
        echo "Today is Tuesday. Buy some food.";
        break;
    case "Wed":
        echo "Today is Wednesday. Visit a doctor.";
        break;
    case "Thu":
        echo "Today is Thursday. Repair your car.";
        break;
    case "Fri":
        echo "Today is Friday. Party tonight.";
        break;
    case "Sat":
        echo "Today is Saturday. Its movie time.";
        break;
    case "Sun":
        echo "Today is Sunday. Do some rest.";
        break;
    default:
        echo "No information available for that day.";
        break;
}
?>

</body>
</html>                                		

Notes: – Switch case statement

  • If the switch case statement (and if) then the switch is equivalent to several nested ones. So this makes it compact or easily readable if use many more switches on the statements.
  • Each case must end with a pause statement. If you do not use the break statement, the next cases will continue to be evaluated.
  • Alternatively, you can use the default case in the switch statement.
  • If none of the cases is true, the default case will be executed.

Conclusion

In this switch case statement tutorial, you have learned what is switch case statement and how to use a switch case statements with examples.

RELATED ARTICLES

Most Popular

Recent Comments