🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

PHP 7 Fundamental Tutorial for Beginners – PHP Switch…Case Statements?

What is PHP Switch…Case Statements

Check Several Possible constant values for an expression.

PHP If…Else Vs Switch…Case

The switch-case statement is an alternative to the if-elseif-else statement, which does almost the same thing. The switch-case statement tests a variable against a series of values until it finds a match, and then executes the block of code corresponding to that match.

Syntax:-

Consider the following example, which displays a different message for each day.

Example:-

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP if-else Statement</title>
</head>
<body>
<?php
$weekday = 2;
switch ($weekday) {
case 1:
echo "Today is Monday";
break;
case 2:
echo "Today is Tuesday";
break;
case 3:
echo "Today is Wednesday";
break;
case 4:
echo "Today is Thursday";
break;
case 5:
echo "Today is Friday";
break;
case 6:
echo "Today is Saturday";
break;
case 7:
echo "Today is Sunday";
break;
default:
echo "Enter the valid week day.";
break;
}
?>
</body>
</html>

The switch-case statement differs from the if-elseif-else statement in one important way. The switch statement executes line by line (i.e. statement by statement) and once PHP finds a case statement that evaluates to true, it’s not only executes the code corresponding to that case statement, but also executes all the subsequent case statements till the end of the switch block automatically.

This Example for Without Break:-

This Example for different case to same Statements

This Example for one case two Statements

This Example For variable not match so print is default

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP if-else Statement</title>
</head>
<body>
<?php
$weekday = 9;
switch ($weekday) {
case 1:
echo "Today is Monday";
break;
case 2:
echo "Today is Tuesday";
break;
case 3:
echo "Today is Wednesday";
break;
case 4:
echo "Today is Thursday";
break;
case 5:
echo "Today is Friday";
break;
case 6:
echo "Today is Saturday";
break;
case 7:
echo "Today is Sunday";
break;
default:
echo "Enter the valid week day.";
break;
}
?>
</body>
</html>

To prevent this add a break statement to the end of each case block. The break statement tells PHP to break out of the switch-case statement block once it executes the code associated with the first true case.

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.