86

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 successfully? If they are identical, is there any preferred standard to indicate successful script completion? I tend to use exit;.

EDIT: All of the answers have "die() and exit() are identical" even though I say that in my question. I updated to the title to hopefully make it clearer that this is NOT my question. I want to clearly indicate success from a command line script.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405

7 Answers7

72

These are all identical. I'm pretty sure die() is just a straight-up alias to exit(), but even if it isn't, it still acts identically.

When one of these functions is given a string argument, it prints out the string before terminating the process. When it encounters an integer under 255, that integer is considered the return code for the process, which gets passed back to the process which invoked the PHP script. This is particularly useful when writing command line applications (PHP isn't web-only!).

As far as the difference between exit, exit(), and exit(0), there really is none. There is definitely no difference between the first two because exit is technically a language construct, not a function, so it can be called with or without parentheses, just like echo. Returning a code of 0 means "this program ran successfully/without errors", and while I don't know what exactly happens when you don't pass an argument, PHP.net says that an argument-less exit indicates success, so I would bet it returns 0, though again PHP.net doesn't show a default for the argument.

ankitr
  • 5,992
  • 7
  • 47
  • 66
AgentConundrum
  • 20,288
  • 6
  • 64
  • 99
  • 3
    `exit(0)` means exit with success, `exit(1)` means exit with fail. – Yousha Aleayoub Aug 13 '17 at 21:05
  • FYI, never explicitly mentioned anywhere (incl. the PHP manual), what the actual exit code would be when calling `exit` (or `die`) with a string parameter. Having seen e.g. the common `die(-1)` idiom, and also `die("Poo!...")` calls a lot, one may not be so sure after all, and anyway: guessing should belong to other professions... So, FTR: it's 0. Which also means that the latter, `die("error!")` would most likely be a bug in CLI scripts, actually! :-o – Sz. Mar 15 '18 at 12:53
6

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.

nsanders
  • 12,250
  • 2
  • 40
  • 47
  • I understand about failure conditions, but I want to indicate success properly. – Explosion Pills Nov 07 '11 at 01:52
  • For normal, browser based interaction just die(). For command line usage of php, do die(0). – nsanders Nov 07 '11 at 01:57
  • It's worth mentioning... die() by itself doesn't indicate an error condition. It simply stops all php script execution. – nsanders Nov 07 '11 at 02:10
  • +1 mentioning command line, too many people forget about command line php – John Magnolia Mar 04 '13 at 11:51
  • @JohnMagnolia: But then he forgot about the same thing immediately below, when saying "will be displayed to the browser when the exit occurs". ;) (Of course, it's displayed in a CLI console, too.) – Sz. Mar 15 '18 at 12:42
4

I would say that in regards with a better semantics die($arg); should be used for an abnormal or unexpected termination, even when -of course- you still have caught it. And exit($arg); should be used for a normal (expected / controlled) end of a process, like in break; for a for or while or a switch structure but with a definitive end.

Nevertheless .. I personally often use a general if { } else { } structure to control different branches of huge processes or output buffering so not having to use "exit" ..

I also use die($arg) in simple error-catching semantics like in

$db = mysql_connect([$args]) or die ($error); ...

jjyepez
  • 352
  • 3
  • 7
1

In some cases when hacking in CLI, we do not want the program to get killed, while not wanting to continue the full execution.

Here the goal is to avoid making api calls to a separate hand-point file. Say I have a nice play button in my interface, that execute system calls.

Example 1: The program get killed after the job , no datas returned. This is not wanted.

if ($_GET["play"] != ""){
  // Some job
  exit; 
}
/* Huge amount of data*/ 

Example 2: The program still runs, feeding the whole data in the GET request. This is unnecessary on this case. This is slowing down the browser with all the data, that he has already.

if ($_GET["play"] != ""){
  // Some job
}
/* Huge amount of data*/ 

Example 3: The program still runs, no data returned as expected, the play command had been executed, but the whole data set get parsed, this is unnecessary job, can slow down php/the machine.

/* Huge amount of data*/ 
if ($_GET["play"] != ""){
  // Some job
}

Example 4: The program still runs, no data returned as expected, the play command had been executed, the whole data had not been parsed, php returned super quickly 200OK with an empty response, as expected. Everyone happy!

if ($_GET["play"] != ""){
  // Some job
  goto END;
}
/* Huge amount of data*/
END: // this a marker, so it ends with colon, not semicolon

Yes! Using GOTO, sometimes is to be considered -as the best to do -!

https://www.php.net/manual/en/control-structures.goto.php

https://i.stack.imgur.com/kQnqC.png

E_net4
  • 27,810
  • 13
  • 101
  • 139
NVRM
  • 11,480
  • 1
  • 88
  • 87
1

die(); is just a synonym for exit(); and is functionally identical.

The standard way is to use exit code zero to signify success, and anything else to denote an error condition.

slugonamission
  • 9,562
  • 1
  • 34
  • 41
0

die() is typically used to kill the script with an error output:

die("There was a fatal error");

where-as exit is typically used as a successful exit (At least in my coding)

The PHP Manual says that the functions are identical.

xthexder
  • 1,555
  • 10
  • 22
0

die is exactly equivalent to exit.

From the manual:

If status is an integer, that value will be used as the exit status..

This is only useful if you have some sort of wrapper that does something based on the exit status. Unless you have a specific need to report an exit code to the outside world, just exit;.

Hamish
  • 22,860
  • 8
  • 53
  • 67