PHP.nl

switch

switch

The statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the statement is for. switch``switch

Opmerking: > Note that unlike some other languages, the statement applies to and acts similar to . If you have a inside a loop and wish to continue to the next iteration of the outer loop, use . continueswitch``break``switch``continue 2

Opmerking: > Note that switch/case does . loose comparison

In the following example, each code block is equivalent. One uses a series of and statements, and the other a statement. In each case, the output is the same.

if``elseif``switchVoorbeeld: structure

<?php
// This switch statement:

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

// Is equivalent to:

if ($i == 0) {
    echo "i equals 0";
} elseif ($i == 1) {
    echo "i equals 1";
} elseif ($i == 2) {
    echo "i equals 2";
}
?>

It is important to understand how the statement is executed in order to avoid mistakes. The statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a statement is found whose expression evaluates to a value that matches the value of the expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the block, or the first time it sees a statement. If you don't write a statement at the end of a case's statement list, PHP will go on executing the statements of the following case. For example:

`switchswitchcaseswitchswitchbreakbreak````php





  Here, if  is equal to 0, PHP would execute all of the echo
  statements!  If  is equal to 1, PHP would execute the last two
  echo statements. You would get the expected behavior ('i equals 2'
  would be displayed) only if  is equal to 2.  Thus,
  it is important not to forget  statements
  (even though you may want to avoid supplying them on purpose under
  certain circumstances).
 `$i``$i``$i``break`


  In a  statement, the condition is
  evaluated only once and the result is compared to each
   statement. In an 
  statement, the condition is evaluated again. If your condition is
  more complicated than a simple compare and/or is in a tight loop,
  a  may be faster.
 `switch``case``elseif``switch`


  The statement list for a case can also be empty, which simply
  passes control into the statement list for the next case.
  
 ```php
<?php
switch ($i) {
    case 0:
    case 1:
    case 2:
        echo "i is less than 3 but not negative";
        break;
    case 3:
        echo "i is 3";
}
?>

A special case is the case. This case matches anything that wasn't matched by the other cases. For example:

`default````php


> **Opmerking:** > Multiple default cases will raise a
>      error.
>    `E_COMPILE_ERROR`

> **Opmerking:** > Technically the  case may be listed
>     in any order. It will only be used if no other case matches.
>     However, by convention it is best to place it at the end as the
>     last branch.
>    `default`




  If no  branch matches, and there is no 
  branch, then no code will be executed, just as if no  statement was true.
 `case``default``if`


  A case value may be given as an expression. However, that expression will be
  evaluated on its own and then loosely compared with the switch value. That means
  it cannot be used for complex evaluations of the switch value.  For example:
  
 ```php
<?php
$target = 1;
$start = 3;

switch ($target) {
    case $start - 1:
        print "A";
        break;
    case $start - 2:
        print "B";
        break;
    case $start - 3:
        print "C";
        break;
    case $start - 4:
        print "D";
        break;
}

// Prints "B"
?>

For more complex comparisons, the value true may be used as the switch value. Or, alternatively, - blocks instead of .

`ifelseswitch````php





  The alternative syntax for control structures is supported with
  switches. For more information, see .
  
 Alternative syntax
  for control structures```php
<?php
switch ($i):
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
    default:
        echo "i is not equal to 0, 1 or 2";
endswitch;
?>

It's possible to use a semicolon instead of a colon after a case like:

<?php
switch($beer)
{
   case 'tuborg';
   case 'carlsberg';
   case 'stella';
   case 'heineken';
       echo 'Good choice';
       break;
   default;
       echo 'Please make a new selection...';
       break;
}
?>