exit, exit(), exit(0), die(), die(0) - How to exit script

后端 未结 7 1962
醉梦人生
醉梦人生 2020-12-24 00:10

I believe that all of these (and even die() or die(0)) are identical. If they are not identical, which is preferred for exiting a script successfu

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 00:44

    As several people have mentioned, die() and exit() are exactly the same.

    If you look at the PHP documentation, there are two options for arguments:

    • An numeric value. This is only useful if you are using PHP from the command line, as opposed to a web server. A value of zero indicates success. Nonzero indicates a failure condition occurred.

    • A string value. This will be displayed to the browser when the exit occurs.

    Instead of die() or exit(), I recommend using exceptions and a custom top-level exception handler to manage failure conditions.

    You have more flexibility that way to do things like automatic error logging. Also, if you're using PHP to implement a JSON API, this exception handler can hand back a valid, error-indicating JSON snippet instead.

提交回复
热议问题