PHP Switch Statement

王林
Release: 2024-08-29 12:40:39
Original
791 people have browsed it

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

Detailed Description of PHP Switch Statement

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';
}
Copy after login
  • 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 a common criterion parameter as input, which will take a set of values upon which we must 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.
  • Now let’s examine the processing order and the elapsed time in the control traversal. Given the input of the age variable, the system evaluates the case expression values against the test value and checks the first case. If the condition is not met, the control proceeds to the following statement, evaluate the following expression and continues searching until it finds the relevant expression.
  • Once it evaluates its test value, the system executes the ‘echo ‘child” statement and proceeds to the next step.
  • Will the control flow default also? As it seems something like a condition that will get executed by default. Well, it is not so. You must see that there is a ‘break’ statement in every case statement block, too; the task of ‘break’ is to take the flow out of the switch context and proceed with the next logical instruction in the program file.
  • The default statement executes only if none of the abovementioned conditions are met. For example, if the age is 24, the output will 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 a 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 data analysis in the system you are deploying.
  • Consider 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;
}
Copy after login

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.

Flow Chart for Switch

The flow chart for the PHP switch is the same as other coding languages’ switch statements, as this is common functionality in every language.

PHP Switch Statement

Examples

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.

Use Case 1

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.

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 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!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template