PHP Doku:: Entspricht exit - function.die.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige GrunderweiterungenMiscellaneous FunctionsSonstige Funktionendie

Ein Service von Reinhard Neidl - Webprogrammierung.

Sonstige Funktionen

<<defined

eval>>

die

(PHP 4, PHP 5)

dieEntspricht exit()

Beschreibung

Dieses Sprachkonstrukt entspricht exit().


9 BenutzerBeiträge:
- Beiträge aktualisieren...
admin at studio-gepard dot pl
25.07.2010 15:23
Example of PHP function with SELECT query using.
It simplifies usage of SQL and makes code much much cleaner. (similar functions can be made for other queries)
<?php
define
("sql_error_reporting",true); //true to display errors, false to hide them

 
function SQLselect ($TABLE,$WHERE){

     if (empty(
$WHERE)){
         
$sql = "SELECT * FROM `$TABLE`";
     }else{
      
$sql = "SELECT * FROM `$TABLE` WHERE $WHERE";
     }
    
$query=mysql_query($sql);
     if (
$query===false){ if (sql_error_reporting) {
die (
"Sql error of <b>".$_SERVER["SCRIPT_NAME"]."</b>. Call to `$TABLE` in ".__FILE__." on line: ".__LINE__."<br>".mysql_error()."<br><b>SQL:</b><br>$sql<br>");
}else{ die(); }  }
    
     return
mysql_fetch_array($query);
  }
?>

What this function does is taking the table and your query, execute the mysql query and provide the result. If error happen during the query, than provide detailed output using code provided by matthias dot schniedermeyer at telefonica dot de
---
Usage:
$id=1;
$result = SQLselect ("pages","`id`='$id'");
---
Error output example:
Sql error of /website/index.php. Call to `pages` in C:\www\website\functions\sql.inc on line: 43
Unknown column 'id' in 'where clause'
SQL:
SELECT * FROM `pages` WHERE `id`='1'
zero dot affect at hotmail dot com
10.05.2010 11:53
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
BJ (php-net at starurl dot com)
2.02.2010 5:45
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].
Damien Bezborodov
21.08.2009 7: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.
watch dot do dot sun at gmail dot com
19.08.2009 15:55
You can use this function for mysql connection.

For example:

<?php

$conn
= mysql_connection("localhost","root",12345678);

if(!
$conn){
die(
"Couldn't connected mysql");
}

?>

So if you can't connect to mysql you will take the message.
ecziegler at gmail.com
12.05.2008 15:06
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.
jbailey at raspberryginger dot com
20.01.2007 18:33
die doesn't prevent destructors from being run, so the script doesn't exit immediately, it still goes through cleanup routines.

28.09.2004 18: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
---------
matthias dot schniedermeyer at telefonica dot de
14.10.2003 16: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__);



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)