ovi.ro

ovi.ro

PHP switch case after default works normally

When you put the a case block after the default block the switch will work normally and fallback to the default only after testing all cases.
The documentation doesn’t specify this directly, but there is a note with an example.
Another thing worth mentioning is that switch does loose comparisons, so be careful about 0, false values

$a = 3;
switch ($a) {
    case 1: echo 'one'; break;
    case 2: echo 'two'; break;
    default: echo 'four'; break;
    case 3: echo 'three'; break;
}
// outputs 'three'

Test yourself on iodine.com