die

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

dieexit işlevinin eşdeğeri

Açıklama

Bir dil oluşumu olup exit() işlevine eşdeğerdir.

add a note add a note

User Contributed Notes 8 notes

up
491
Hayley Watson
12 years ago
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.
up
77
Damien Bezborodov
15 years ago
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.
up
54
jbailey at raspberryginger dot com
17 years ago
die doesn't prevent destructors from being run, so the script doesn't exit immediately, it still goes through cleanup routines.
up
4
atesin gamil com
2 years ago
"... OR DIE..."

<?php
    do_something
($with_this) or die ('oops!');
?>

(in php, "die" is actually an alias of "exit")

beware until this is often used as an error handling statement actually isn't... is just the "or" boolean operator overloaded (short circuited)

it won't supress any error message, won't prevent you for fatal errors or crashes, is not an alias for "try/catch", and could has unpredictable results... it just means something like this:

- execute the first statement ""do_something($with_this)"" normally
-- if the statement results TRUE: no need to execute the second statement so skip it
-- else if returns FALSE... well i am not sure about the result of the whole expression, so continue with second statement ""die('cuack!)""

so you can't rely on all statements will suppress error messages or crashes, or will always return "false" on error or undesirable behavior...

for this better use the error skipping operator "@" "if" and "exit" commands, or use "try/catch" blocks
up
13
info at alzlper dot com
6 years ago
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')));
up
3
smcbride at msn dot com
3 years ago
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.
up
-8
stysan
2 years ago
The "die" function is good to use when an error occurs and you need to instantly leave, and the "exit" function is good to use when a program is done without any errors. Why that? So you can search dies in the program and debug better.
up
-26
Anonymous
3 years ago
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);
To Top