Introduction to PHP Switch Statement
If we talk in generic coding terminologies then being a novice to coding, you would have seen “if” statement to handle condition checks and do some action on their validations, now let’s take a case that you are writing logic for traffic light systems design and if you look to proceed with the standard if conditions then probably you would end up with one “if”, one “else if or if” and one “else” statement, and if any other synonymous kind of business logic appears where such criterions are high in number and they belong to the same category, then code won’t appear good and for that we have “switch” statement, where you just need to write this statement once only and describe certain cases associated under a common category and business logic to be implemented in association with that.
Detailed Description of PHP Switch Statement
Let’s see a PHP snippet where we are having a range of age and a corresponding message is being displayed to represent category of those people.
$age = '7-12'
switch($age)
{
case '0-1': echo 'it is a baby';
break;
case '2-3' : echo 'toddler';
break;
case '4-6' : echo 'infant';
break;
case '7-12': echo 'child';
break;
default : echo 'others';
}
- So you may have got a rough idea after seeing the example displayed above, the example carries the implementation of such a condition using just one ‘switch’ statement rather than putting ourselves into multiple if and else statements.
- The Switch takes common criterion parameter as input, which is going to take a set of values upon which we need to apply the conditional evaluation for business logic implementation.
- As in the above case, the age variable shows that the age range mentioned matches ‘7-12’, so we will get ‘child’ in the output.
- Let’s now see the order of processing and how much time will be elapsed in the traversal of control. As the age variable is provided as input, the case expression values are evaluated against the test value, the first case is checked, then the condition is not met, control flows to next statement performs synonymous kind of evaluation and keeps on hunting till it gets its relevant expression.
- Now once it gets its test value evaluated, the echo ‘child’ statement gets executed and then?
- Will the control flow to default also? As it seems something like a condition that will get executed by default. Well, it is not so. You must see that in every case statement block there is a ‘break’ statement too, the task of ‘break’ is to take the flow out of switch context and proceed with the next logical instruction in the program file.
- Default statement gets executed only in case if none of the conditions mentioned above are met, like if I mention the age to be 24 years, then the output will appear to be ‘others’.
- Hence it’s logical to place the default statement at the end of the file.
- This order of placement matters a lot while you write code, and you should be well aware of the kind of input data that you will be getting mostly as test condition, its better to keep that case at the top, so that maximum users get the result as early possible with first line only. This could be done after analysis of data in the system you are deploying.
- Pay a little thought on fact, why there is no break in the default statement, the above description carries the answer although.
Syntax
switch (testvalue) {
case label1:
code to be executed if testvalue = label1;
break;
case label2:
code to be executed if testvalue = label2;
break;
case label3:
code to be executed if testvalue = label3;
break;
default:
code to be executed if testvalue is different from above;
}
We have already shared a program in the above section on this logic only, refer to that for better understanding with a use case.
Flow Chart for Switch
The flow chart for PHP switch is the same as other coding languages’ switch statements as this is common functionality in every language.
Examples
Kindly refer to the example shared in the details section, which carries detailed information about working, and let’s take here some application use cases for better clarity of the picture.
Use Case 1
Let’s say you are gathering the data related to students who have birthday’s in each of the respective months of calendar year, here you can put month into switch criteria and can have 12 different arrays to store data of students pertaining to different months, hence keep on adding data to each of the array as the condition is met and for a count of 5000 students in a school, likely your all arrays will get occupied.
Use Case 2
Let’s talk about small scale design of a calculator where you need to perform addition, subtraction, and multiplication like operations, in a switch, you can take the name of the operation, validate it against case labels, and once met, the business logic there would return the value of output based on respective calculations.
Conclusion
We saw the cases where the increase in a number of conditions against a category if gets increased then it’s better to adapt with a switch statement, it makes code more clear, readable, and can make it fast too based on data analysis and placement of logics accordingly. We saw syntax for implementation in PHP, for example, and few relevant use cases.
Recommended Articles
This is a guide to PHP Switch Statement. Here we discuss the detailed description of PHP switch statements, a flow chart for a switch, and an example. You can also go through our other suggested articles –
5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses