PHP switch case selection construct

Condition and choice are key language constructs. Correct decision making is the basis of a high-quality algorithm and clear, understandable code. Correct design of the algorithm and the correct use of comments are related to stability and reliability factors.

switch case php

Modern programming is still not without rigidity. Once written code can adapt to changing the conditions of a task only with the help of a competent specialist. But still, the level of code mobility is determined by the qualification level of its author.

Selection Design Syntax

The switch case selection construct PHP is very simple to write, correctly executed, and convenient in practice. The example suggests a function that draws text with HTML tags. If the first parameter contains 'bold' or 'italic', then the result of the function wraps the second parameter with the tags 'strong' or 'i'.

Author's example is not a picture

The result in the browser displays the contents of the second parameter in bold or oblique style. Any other value of the first parameter is interpreted as color and wraps the second parameter with the tag 'font'.

This function does not check the correctness of the initial parameters and can be used only in that part of the code where the parameters cannot go beyond the permissible limits.

In the proposed example, the use of the 'break' operator is redundant, since as a result of the execution of any condition, the function will exit the operator by the 'return' operator.

PHP construction description: switch case

The condition that is checked is written in parentheses after the 'switch' keyword. The body of the construction consists of sections 'case' and one block of statements 'default', which is executed only if none of the blocks 'case' have worked.

There is a very significant nuance. The block of 'case' statements begins after the ':' character and ends with the 'break' statement. The presence of the 'break' operator is optional. If it is not, then the following 'case' condition will be checked. Checking conditions is always performed before the first match. Naturally, the conditions here are only '=='.

Author's example is not a picture

Each section of the 'case' is executed only if the condition is met, but the block of 'default' statements will always be executed if there is a skip of the 'break' operator in that section of the 'case' that worked, or none of the conditions match.

In fact, in PHP: a switch case with several values ​​has several blocks of statements. To formally follow the syntax, each statement block must be completed with a 'break'.

As soon as the condition coincides, the corresponding section is executed and, if there is no 'break' in it, everything that follows it. In the example, the value of the variable '$ x' is 1 and there is not a single 'break', therefore the result = '-one - two - default-'.

Nesting conditional structures

The language does not limit the developer in the level of nesting. It is allowed to nest switch case. PHP also does not limit the programmer in choosing the operators that can be used inside the case.

php switch case

This allows you to make beautiful and easy to read algorithms. For example, you need to recognize a CSS rule table. You can immediately set the switch case PHP to recognize classes and identifiers, then recognize the rules themselves. This is an obvious solution, but cumbersome. Both classes and identifiers use similar rules.

php switch case multiple values

It is more convenient to perform rule recognition through a switch case. You can include a function in PHP that will access rule recognition. You can go further. Many rules allow similar meanings. Arguing in this way, try to build the reverse process: the switch case in PHP will be executed in the form of functions, the first works at the level of values, the second at the level of rules, and the third is called from the level of a specific class or identifier.

Source: https://habr.com/ru/post/K9138/


All Articles