35

Possible Duplicate:
what are the differences in die() and exit() in PHP?

I guess the main question is what is the difference between the 3 exactly?

What is the correct semantic usage for each one of these?

From what I see return false; can discontinue a function whereas die(); and exit(); will prevent any further code running at all.

Is this correct?

Community
  • 1
  • 1
daryl
  • 14,307
  • 21
  • 67
  • 92

2 Answers2

38

die() and exit() are precisely identical; they halt the entire PHP program and return to the OS. They're two different names for the same function.

return, on the other hand, ends a function call and returns to the caller. At the end of a program, return sets the status value that is returned to the OS; the program is going to exit no matter what.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • 1
    So what's the need for two functions which do exactly the same thing, there must of been a reason for this. – daryl Dec 13 '11 at 14:23
  • 11
    Just "syntactic sugar". Perl has `die()`, and many other languages have `exit()`, so by having both, PHP tries to make it easier to transition between languages, I suppose. – Ernest Friedman-Hill Dec 13 '11 at 14:25
  • 1
    @Brogrammer not quite. Not a significant reason at least. It can be some compatibility issue, a legacy issue, etc. Whatever reason it can be - it doesn't matter anyway. – Your Common Sense Dec 13 '11 at 14:26
  • 1
    They're not TWO function indeed, `die` is just an alias to `exit` –  Dec 13 '11 at 14:27
  • 3
    Not correct. `die()` is same as `exit()` but not vice versa. You can pass a param for `exit()`: either `exit(status code number)` or `exit(string status)`, which is passed out from PHP to OS or whatever. – d.sergeiev May 28 '14 at 11:50
  • So, a `return false` will stop the function and if the client presses the function button, the function will start again ? – Mihir Ujjainwal Feb 25 '15 at 16:37
  • @d.sergeiev you can also pass parameters to `die()`, i.e `die("error occurred")` will print "error occurred" before exiting. – JStephen Apr 17 '17 at 16:10
11

According to docs PHP: exit Manual die() is an alias to exit() so they do the same function and that is to END the script.

The return statement ends a function and not the entire script, and returns the value that you choose.

itoctopus
  • 4,133
  • 4
  • 32
  • 44