PHP Tutorial on Switch Statements with Stone River eLearning
Taking our discussion on the flow control statements to the next level, in this blog post on PHP Programming, we are going to discuss how switch statements are executed in PHP and how useful are they in the websites that we see on the internet. A lot of students usually have this doubt that, ‘When I already have IF, ELSEIF, WHILE, so why do I need switch statement in my codes?’ The reason why PHP or any other programming language has multiple looping structures for performing the same operation is because performance and efficiency have become important aspects of web programming in the current time. Imagine you type the URL of a website on your browser and then scrolling through the different pages on that website needs a minute or two. The popularity of internet has seen unprecedented growth in the past 10 years, however; with this growth the expectations of users to get a fast and accurate response from their web service, has also grown at a similar pace. Therefore, instead of checking the same condition multiple times on a ‘IF-ELSEIF’ statement, it is better to have a switch statement in your PHP script and do the operation swiftly and cleanly.
Why do I need switch statement?
As we mentioned in the above paragraph, even though ‘IF’, ‘ELSE-IF’ are often sufficient to check any condition and execute the operation corresponding to it, but in search of an optimized solution, switch statement are required. Looking at the execution cycle of IF-ELSEIF statement in the previous blog posts, you might have felt that even though the flow control statement is of great utility in case of multiple outcome problems but its use has made the problem look clumsy, and for bigger problems it might get difficult to find which ELSEIF scenario tracks to which condition. Well, you got such a thought in your mind then pat your back because the developers of PHP got a similar one and therefore they introduced the Switch statement for making a PHP script easier to write and to understand. Let us see the same example that we used for IF-ELSEIF statement and see how it changes the PHP code when we impose the switch statement to it.
Eg:
<?php $day = 'Monday'; //variable declaration switch ($day) //Switch statement initialisation { case “Monday”: // Case for each outcome possible echo 'Today is Monday.'; break; case “Tuesday”: echo 'Today is Tuesday.'; break; case “Wednesday”: echo 'Today is Wednesday.'; break; case “Thursday”: echo 'Today is Thursday.'; break; case “Friday”: echo 'Today is Friday.'; break; case “Saturday”: echo 'Today is Saturday.'; break; default: // Default condition for any outcome other than the one mentioned echo ‘Invalid Entry’; } ?>
NOTE:
- Each condition in the script can be grouped as a different case using the switch statement and then if required, a different operation can be set to each of these cases.
- The script looks cleaner, more organised and structured after switch statement is used.
- The default condition in the switch statement is optional and can be skipped.
- Using the keyword ‘break’ is a critical part of the switch statements and it allows the processor to understand that the current case has ended and a new case will start now.
GET YOUR FREE PHP EBOOK!