dismiss Step into the future! Click here to switch to the beta php.net site
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

eval> <defined
[edit] Last updated: Fri, 28 Jun 2013

view this page in

die

(PHP 4, PHP 5)

dieEquivalent to exit

Description

This language construct is equivalent to exit().



eval> <defined
[edit] Last updated: Fri, 28 Jun 2013
 
add a note add a note User Contributed Notes die - [9 notes]
up
4
DAFox
9 months ago
Something to keep in mind OWASP states that "Attackers love the extra information error messages provide".  They ask
"Is your error handling set up to prevent stack traces and other overly informative error messages from leaking"
OWASP has guidelines on their site about good error messaging. Something everyone should consider when creating sites.
up
1
Anonymous
8 years ago
Perhaps the Coldfusion query below can be answered as follows:
---------
(From the "User Contributed Notes" for the PHP construct "exit")

nospam at mydomain dot com
27-Sep-2004 10:12
Using return instead of exit is to prefer when you want the script that you have included inside another script to die but continue to execute the main script.
// Radcliff
---------
up
9
jbailey at raspberryginger dot com
6 years ago
die doesn't prevent destructors from being run, so the script doesn't exit immediately, it still goes through cleanup routines.
up
6
Damien Bezborodov
3 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
14
Hayley Watson
10 months 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
1
matthias dot schniedermeyer at telefonica dot de
9 years ago
To get a perl-like die you can/should append this snipped
 . " File: " . __FILE__ . " on line: " . __LINE__
e.g.
die ("Error" . " File: " . __FILE__ . " on line: " . __LINE__);
up
0
BJ (php-net at starurl dot com)
3 years ago
Correction: when using trigger_error() to throw a fatal error, make sure you include "E_USER_ERROR" as the error type, like so:

    @statement or trigger_error("Error msg", E_USER_ERROR);

Otherwise, trigger_error() defaults to a non-fatal Warning, so script execution won't be halted.

Sorry for omitting that! [Note to Ed: feel free to correct post and remove this correction].
up
-1
zero dot affect at hotmail dot com
3 years ago
I was making a few functions and i thought it would be easier to use a single function to close off my code, The following is a example of it working i am sure if you include footer code when exiting code all the time you'll understand the purpose of the following function.

<?php
function error_msg($text) {
    
# add other stuff you may want here
    
$hello_var = 'hello'; //example of addon to beginning
    
$goodbye_var = 'goodbye'; //example of addon to end
    
die($hello_var.'<br />'.$text.'<br />'.$goodbye_var);
}

error_msg('how are you?');
?>

Outputs:
hello
how are you?
goodbye
up
-2
ecziegler at gmail.com
5 years ago
If you are using auto append file (auto_append_file ="_end.php";) on your php.ini, be carefull on using die anywhere you want your appended file to be called. Souds obvious now but took me a good 1 hour to realise why one script was not appending end file as expected.

I solved using `return` instead; it works anywhere not just inside functions and the effect is pretty much the same as die.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites