php – What is the difference between Switch, Case and If, Else?

Question:

I would like to know the difference between switch .. case and if .. else .

Which offers a better "performance"?

Answer:

In short, the switch control structure is an if one to operate on the same input variable or expression.

There is not exactly a comparison between the two to determine their performance given the objective of each being distinct.

What exists is the correct use of the most adequate control structure for our purpose.

switch

We should use it when we want to compare the same variable or expression with multiple options:

The switch statement is similar to a series of IF statements on the same expression. On many occasions, you may want to compare the same variable (or expression) with many different values, running a different piece of code depending on which value it fits. This is exactly what the switch statement does.

Example in 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 should use it when we want to perform a series of different checks.

The if construct is one of the most important features of many languages, PHP include. It allows the conditional execution of code fragments.

Example in 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 making use of different expressions to execute code accordingly.

Comparison

In order to be able to compare the two, we would have to make use of one of them less adequately:

Use switch less properly:

Example in 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 observe that the input value does not matter when we use the switch as if it were an if .

Use if less properly:

Example in 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 design to make comparisons much more complex than this.

Scroll to Top