die

(PHP 3, PHP 4, PHP 5)

die -- Equivalent to exit()

Description

This language construct is equivalent to exit().


add a note add a note User Contributed Notes
massimo dot piano at hotmail dot it
04-May-2006 03:37
You may use die() or the equivalent exit() after a redirection ( via the header statement )

<code>
header("location : http://www.differentpage.com");
die();
<code>

this may seem strange but you have to rememeber that header affects the client not the server script ( in fact it will continue to be executed if you dont stop it)
of course you can let the script continue but someone forget this behaviour and get "strange errors"
enumeration at gmail dot com
30-Dec-2005 04:48
It's very useful on websites to have all your code in functions, then use "return" instead of die. For example:

function log_in() {
   if(!isset($_POST[password])) {
       print "You must supply a password.";
       return;
   }

// rest of the login function here
}

This way it exits out of the function, gives an error message, but doesn't keep the website from finishing loading.
cairey at comcast dot net
08-Jun-2005 04:30
The die function can also be used within variables and arrays, as long as it is not within quotes.

Example:

$_POST[die];

This will quit the script as it would if you just called die; You can, however, send die as a POST variable as seen below:

$_POST['die'];
matt at hehe dot gov
22-Mar-2005 12:17
If you want to issue die on a file that is inside an include, it will keep everything else from running through and finishing.
You can prevent this by using return;. It ends the current file but lets the others keep going.
29-Sep-2004 12:36
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
---------
louis t mulliemedia.com
24-Jun-2004 02:13
In regards to xagent_ at hotmail dot com
03-Sep-2002 05:22

A useful way of preventing this is to wrap the die function :

<?php
function die_script($errmsg)
{
 print
$errmsg . "</body></html>";
 die;
}
?>
21-Apr-2004 01:46
die(__LINE__); won't say the line number because die() wants a string. do die(''.__LINE__); or die(__FUNCTION__ . ':' . __LINE);
matthias dot schniedermeyer at telefonica dot de
14-Oct-2003 10:29
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__);
xagent_ at hotmail dot com
04-Sep-2002 08:22
Note that using the die() function will stop the entire script after that point from being run, so if you issue the die function within a open HTML tag, the HTML tag will remain open, which can seriously effect the browsers output to the user, expecially if its used inside an open <TABLE> tag.