If we talk in generic coding terminologies, then being a novice to coding, you would have seen an “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 criteria are high in number. The code won’t appear good if they belong to the same category. For that, we have a “switch” statement, where you 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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Let’s see a PHP snippet where we have a range of age, and a corresponding message is displayed to represent those people’s categories.
$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'; }
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 a better understanding of a use case.
The flow chart for the PHP switch is the same as other coding languages’ switch statements, as this is common functionality in every language.
Kindly refer to the example shared in the details section, which carries detailed information about working, and let’s take some application use cases here for better clarity of the picture.
Let’s say you are gathering the data related to students who have birthdays in each of the respective months of the calendar year; Here, you can include a month as a switch criteria and create 12 different arrays to store data of students corresponding to each month. As the condition is met, you can continuously add data to each of the arrays. All the arrays will likely become occupied by a total of 5000 students in a school.
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.
We saw the cases where the number of conditions against a category increases; then it’s better to adapt with a switch statement; it makes code clearer and readable and can make it fast, too, based on data analysis and placement of logic accordingly. We saw syntax for implementation in PHP, for example, and a few relevant use cases.
The above is the detailed content of PHP Switch Statement. For more information, please follow other related articles on the PHP Chinese website!