6

I've been thinking to much...

In the area of switch case is break; required after die()

Example:

switch($i){

     case 0:
          die('Case without break;');

     case 1:
          die('Case with break;');
          break;

}
Evan Stoddard
  • 718
  • 1
  • 7
  • 21
  • 2
    It's not necessary, because the script could NEVER actually reach the break line, because you just killed the script with the `die()` call. It's kind of like ordering food for a dead person. Maybe it has some meaning for you, but that person's not going to be around to eat it. – Marc B Aug 09 '13 at 19:20
  • Thanks! That's what I figured. I just thought I'd ask around and see what other people had to say about it. Nice analogy by the way. – Evan Stoddard Aug 09 '13 at 19:22

3 Answers3

6

die() is just an alias for exit(), where exit() will terminate the program flow immediately. (Shutdown functions and object destructors will still get executed after exit())

And no, it is not a syntax error to omit the break, on the contrary there are many useful cases for omitting the break. Check the manual page of the switch statement for examples.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Thanks for that additional info! I was not aware of the relationship. – Evan Stoddard Aug 09 '13 at 19:06
  • Yeah, found this interesting once too. :) I don't know why they have so much aliases in PHP. Also you should know that if you pass a string to `die()` - what is seen in most application scenarios - it has a direct influence on the return value. If you craft a framework a I would use a function like `panic($string)` which will print the message using `echo` and afterwards calls `exit(1);` – hek2mgl Aug 09 '13 at 19:54
  • Was not aware of that either. – Evan Stoddard Aug 09 '13 at 19:55
  • 1
    Check this it is funny (if you not have to debug this) : `die('001 Script Error');` will have a return value of `0 - Success` as the string will be converted to a number: `0`. Had this once, debugging this made me going crazy!!! %) – hek2mgl Aug 09 '13 at 19:57
4

it's not required. Even for the switch break is not mandatory. If no break is in one case, it just keeps executing the next.

but after die, it makes no difference, since die terminates the program execution. Just hope you don't plan to use die inside some cases.

Aris
  • 4,643
  • 1
  • 41
  • 38
1

Syntactically, it is not required, but it won't be executed since die() causes the execution to stop.

Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85