Blog de programación, errores, soluciones

Chose Language:
comments
Author: Admin/Publisher |finished | checked

PHP switch statement 

Sometimes we want to compare the same variable with different values, and that’s where the PHP switch statement comes in.

This switch section was created due to different user requests, as they mentioned that the post about control structures is lengthy. Nevertheless, I recommend reading it.

In the following example, we compare the variable $i with the values 0, 1, 2.

switch example
switch ($i) {
    case 0:
        echo "i es 0";
        break;
    case 1:
        echo "i es 1";
        break;
    case 2:
        echo "i es 2";
        break;
}


See that case indicates the case, for example case 0: indicates that if $i is 0, the code that follows will be executed.

break

As you can see, within switch, break is used, this is to prevent the code from continuing to execute.

If a break is not used, the statement will continue to execute the next case as well.

Switch without break
$i=1;
switch ($i) {
    case 0:
        echo "i es 0";
        break;
    case 1:
        echo "i es 1";
    case 2:
        echo "i es 2";
        break;
}
i es 1i es 2

default

In PHP, within switch, in addition to the case cases, we also have default, which always goes at the end of the switch. The code for default will be the code that will be executed by default when a value is not found that corresponds to a case.

Switch using default
$variable = "valor";

switch ($variable) {
    case "opcion1":
        echo "Estás en la opción 1";
        break;
    case "opcion2":
        echo "Estás en la opción 2";
        break;
    default:
        echo "No estás en ninguna de las opciones anteriores";
        break;
}

Comparison in Switch

Something we need to keep in mind is that when we use case x:, x doesn’t have to be a value per se, but it can also be an expression. For example, suppose we are comparing ages:

Comparison in switch
$age = 45;

switch (true) {
    case ($age > 60):
        echo "Edad mayor a 60 años";
        break;
    case ($age > 50):
        echo "Edad entre 51 y 60 años";
        break;
    case ($age > 40):
        echo "Edad entre 41 y 50 años";
        break;
    case ($age > 30):
        echo "Edad entre 31 y 40 años";
        break;
    case ($age > 20):
        echo "Edad entre 21 y 30 años";
        break;
    default:
        echo "Edad menor o igual a 20 años";
        break;
}

When should we use Switch in PHP instead of if?

Switch can be useful in cases where we have to make a large number of comparisons. I would say that if you have more than 4 comparisons, you should start using switch.

In cases of large comparisons, switch can be faster than if, and this is another feature you should consider.

Finally, you should consider readability. Keep in mind that another programmer may have to read your code.

Please note that I have separated this section from [es]https://blastcoding.com/estructuras-de-control-php/ some control structures should have their own post to explain them as well as possible
Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category

Comments