# switch() and default:

- Thread: https://externals.io/message/13221
- Original archive: https://news-web.php.net/php.internals/13221
- Started by Frank M. Kromann on 2004-10-08T03:24:16+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #13221 — Frank M. Kromann — 2004-10-08T03:24:16+00:00

URL: https://externals.io/message/13221#13221

Hello Everyone,

I just discovered a small thing in the switch() statement. The position of
the default: clause has to be at the end of the code:

$a = 1;
switch ($a) {
   default :
   case 0 :
      $b = 1;
      break;
   case 1 :
      $b = 2;
      break;
}
echo $b; // should print 2 but it prints 1

$a = 1;
switch ($a) {
   case 1 :
      $b = 2;
      break;
   default :
   case 0 :
      $b = 1;
      break;
}
echo $b; // prints 2 as expected.

This is tested on Linux with PHP5 CVS-HEAD

What changed ?

- Frank

---

## #13222 — Andi Gutmans — 2004-10-08T04:59:12+00:00

URL: https://externals.io/message/13221#13222 · In reply to: #13221

It's always been like that and has been documented for ages in the manual.

Andi

At 08:24 PM 10/7/2004 -0700, Frank M. Kromann wrote:
>Hello Everyone,
>
>I just discovered a small thing in the switch() statement. The position of
>the default: clause has to be at the end of the code:
>
>$a = 1;
>switch ($a) {
>    default :
>    case 0 :
>       $b = 1;
>       break;
>    case 1 :
>       $b = 2;
>       break;
>}
>echo $b; // should print 2 but it prints 1
>
>$a = 1;
>switch ($a) {
>    case 1 :
>       $b = 2;
>       break;
>    default :
>    case 0 :
>       $b = 1;
>       break;
>}
>echo $b; // prints 2 as expected.
>
>This is tested on Linux with PHP5 CVS-HEAD
>
>What changed ?
>
>- Frank
