I would like to know the difference between switch .. case
and if .. else
.
What offers better "performance"?
I would like to know the difference between switch .. case
and if .. else
.
What offers better "performance"?
In short, the control structure switch
is a if
to operate on the same variable or input expression.
There is no such thing as a comparison between the two to ascertain their performance given the goal of each being distinct.
What exists is the correct use of the control structure best suited for our purpose.
switch
We should use when we want to compare the same variable or expression with several options:
The
switch
statement is similar to a series ofIF
instructions on the same expression. On many occasions, you may want to compare the same variable (or expression) with many different values by running a different part of the code depending on which value it fits. This is exactly what theswitch
statement does.
Example on Ideone .
switch($a) {
case 1: {
echo 'Variável A é igual a 1';
break;
}
case 2: {
echo 'Variável A é igual a 2';
break;
}
default: {
echo 'A Variável A não é igual a 1 nem igual a 2';
}
}
We are buying $a
with a number of different values.
if
We must use it when we want to perform a series of different checks.
if
construction is one of the most important features of many languages, including PHP. It allows conditional execution of code fragments.
Example on Ideone .
if ($a > $b) {
echo "A é maior que B";
}
else if ($a < $b) {
echo "A é menor que B";
}
else if ($a == $b) {
echo "A é igual a B";
}
else {
echo "A é alguma coisa não comparável com B";
}
We are using different expressions to execute code accordingly.
In order to compare the two, we would have to make use of one of them less appropriately:
Use switch
less appropriately:
Example on Ideone .
switch($a) {
case ($a > $b): {
echo "A é maior que B";
break;
}
case ($a < $b): {
echo "A é menor que B";
break;
}
case ($a == $b): {
echo "A é igual a B";
break;
}
default: {
echo "A é alguma coisa não comparável com B";
}
}
echo PHP_EOL;
switch("batata frita") {
case ($a > $b): {
echo "A é maior que B";
break;
}
case ($a < $b): {
echo "A é menor que B";
break;
}
case ($a == $b): {
echo "A é igual a B";
break;
}
default: {
echo "A é alguma coisa não comparável com B";
}
}
We can see that the input value does not matter when we use switch
as if if
was.
Use if
less appropriately:
Example on Ideone .
if ($a == 1) {
echo 'Variável A é igual a 1';
}
else if ($a == 2) {
echo 'Variável A é igual a 2';
}
else {
echo 'A Variável A não é igual a 1 nem igual a 2';
}
We can see that we are comparing the same variable with different values, making use of a control structure draws to make comparisons far more complex than this.
The comparison should actually be made between switch
and if
. The case
is part of the switch
construct to identify each block. else
already has a similar shape but works differently.
The switch
is used to compare the equality of values with an expression (usually a variable is used). It can not do other types of comparison or relationship between data. It is quite simple, you establish an expression in the switch
that is one side of the comparison and in each case
will be placed a fixed value, it must be literal - that will be the other side of the comparison. He will try to evaluate all the values that establish this equality. To avoid all subsequent evaluations it is necessary to say this explicitly in the code, probably with break
.
The if
evaluates any condition, it is possible to use complex expressions establishing comparisons and relations that in the end result in true or false. If the result is true, the next block of code will be executed, otherwise the code block set to else
will be executed. You still have the elseif
issue that was already discussed in another question . Running a block, it does not evaluate the condition, much less execute the others.
The trend is switch
to be slightly faster because it is probably implemented with a lookup table . And the comparison would only be fair in if
the expression also makes a simple equality expression. Any other expression will make the comparison pointless since they do different things. Including there compare with different expressions. So you can compare the performance of these examples:
switch ($var) {
case 0:
//faz algo
break;
case 1:
//faz algo
break;
case 2:
//faz algo
break;
case 3:
//faz algo
break;
}
E
if ($var == 0) {
//faz algo
} elseif ($var == 1) {
//faz algo
} elseif ($var == 2) {
//faz algo
} elseif ($var == 3) {
//faz algo
}
In other ways, it is not possible.
Even though it makes some difference, it will be very small, essentially insignificant, even more so in PHP. Not a recommended concern, on the contrary, the choice of one or the other must be out of necessity. switch
helps indicate that you are looking for a value among a list of them. (I do not have time to do tests right now, but they're pretty much identical.)
Since PHP 5.5 in some cases it may be more interesting to do ['stack', 'overflow'][$x]
than
switch ($x) {
case 0:
echo 'stack';
break;
case 1:
echo 'overflow';
break;
}