Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: exit - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

get_browser" width="11" height="7"/> <eval
Last updated: Thu, 31 May 2007

view this page in

exit

(PHP 4, PHP 5)

exit — メッセージを出力し、現在のスクリプトを終了する

説明

void exit ( [string $status] )
void exit ( int $status )

スクリプトの実行を終了します。

パラメータ

status

status が文字列の場合は、この関数は終了直前に status を表示します。

statusinteger の場合は、 その値は終了ステータスとしても用いられます。終了ステータスは 0 から 254 までの値でなければなりません。終了ステータス 255 は PHP に予約されており、使用してはいけません。ステータス 0 は、 プログラムを正常終了させる際に使用します。

注意: PHP >= 4.2.0 ではstatusinteger の場合それを表示しません。

返り値

値を返しません。

例 1307. exit() の例

<?php

$filename
= '/path/to/data-file';
$file = fopen($filename, 'r')
    or exit(
"ファイル ($filename) をオープンできません");

?>

例 1308. exit() でステータスを指定する例

<?php

// 正常終了
exit;
exit();
exit(
0);

// エラーコードつきの終了
exit(1);
exit(
0376); // 八進数

?>

注意

注意: これは、関数ではなく 言語構造のため、可変関数 を用いて コールすることはできません。

注意: この言語構造は、die() と等価です。

参考

register_shutdown_function()



get_browser" width="11" height="7"/> <eval
Last updated: Thu, 31 May 2007
 
add a note add a note User Contributed Notes
exit
dan
12-Jul-2007 07:11
In relation to the below comment, you may find that using the following may be more appropriate:

<?php
# ... user has pressed log out, cookies have been wiped, etc.

// Stay on the same page at time of logout (useful if a page is also available to anyone who isn't logged in
header ("Location: http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);

?>

Of course $_SERVER['PHP_SELF'] can be omitted if you wish to redirect to the root directory of the site (http://www.example.com) or a path of your choosing can be used instead.

Alternatively, if you've a solid system implemented and logged out users can access the page too, you can continue with showing the page without using exit() or header().
brianbtn at bellsouth dot net
11-May-2007 11:14
how do I redirect back to the login page after someone clicks logout by using the passwordProtectedsite.php?logout=1

HERE IS THE CODE FROM PASSWORD_PROTECT.PHP

// logout?
if(isset($_GET['logout'])) {
  setcookie("verify", '', $timeout, '/'); // clear password;
  exit("Logged Out");
}
Roumen Semov
17-Feb-2006 04:54
Please note in PHP "exit(0)" or simply "exit" returns true.
Any other value but zero will return false. This is good to know in case you are writing command-line php scripts where you need the result of the php script to determine if the next script will run. Example:
shell> ./my_php_script && echo "It ran successfully!"
If you know my_php_script can break somewhere you could do a conditional with an "exit(-1)" and then if the script breaks the command after the && will not execute.
09-Feb-2006 10:26
The note above is irrelevant, because a simple "return" would  abort the template included by include or include_once!
francois at bonzon dot com
25-May-2005 10:42
With output buffering on, when sending headers that shouldn't have any data after them, be sure to erase the buffer before exit()ing the script! Like this:

<?php
ob_clean
();
exit();
?>

If you don't erase the buffer, in case it was not empty, after sending the headers PHP will still send the buffer content to the browser.

I had Firefox show strange behavior with some of my pages, that it took me quite some time to debug. It was simply because my script was returning 304 Not Modified headers along with the start of an HTML page.
nospam at mydomain dot com
27-Sep-2004 06: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
emils at tvnet dot lv
24-Aug-2003 12:14
Note, that using exit() will explicitly cause Roxen webserver to die, if PHP is used as Roxen SAPI module. There is no known workaround for that, except not to use exit(). CGI versions of PHP are not affected.
mbostrom at paragee dot com
27-Feb-2003 05:45
In PHP 4.3.1 (and possibly 4.3.0), running scripts from the command line works a lot better.  This is probably because 4.3.x has a whole new CLI mode.

Specifically, exit status is now returned (to the shell) as you would expect.  This is a godsend for writing embedded email processing scripts, as much email infrastructure (fetchmail, qmail, mutt, etc.) is dependant upon correctly returned status codes, and the inability to return a status code (as in PHP 4.2.x) is an insurmountable obstacle.

$_SERVER["argv"] is also always available in 4.3.x, I think, whereas in 4.2.x php.ini could prevent it from being available.

(On the downside, I had to ./configure --without-mysql in order to get 4.3.1 to compile on RedHat 8.0.  Otherwise there was what looked like a fatal compile warning (that I might also have been able to ignore somehow).

The "fatal warning" FYI:
ext/mysql/libmysql/my_tempnam.o: In function `my_tempnam':
ext/mysql/libmysql/my_tempnam.c:103: the use of `tempnam' is dangerous, better use `mkstemp'

Changing the code from tempnam to mkstemp would probably not be overly complicated, but it is non-trivial.)
shaun at NOshatSPAM dot net
09-Aug-2002 08:13
return may be preferable to exit in certain situations, especially when dealing with the PHP binary and the shell.

I have a script which is the recipient of a mail alias, i.e. mail sent to that alias is piped to the script instead of being delivered to a mailbox. Using exit in this script resulted in the sender of the email getting a delivery failure notice. This was not the desired behavior, I wanted to silently discard messages which did not satisfy the script's requirements.

After several hours of trying to figure out what integer value I should pass to exit() to satisfy sendmail, I tried using return instead of exit. Worked like a charm. Sendmail didn't like exit but it was perfectly happy with return. So, if you're running into trouble with exit and other system binaries, try using return instead.
iamfast at tampabay dot rr dot com
14-Jul-2002 12:12
If you are working with images or something of the sort that is not html, and use auto appending, call exit before you close your php tag, so that the footer is not included, corrupting the end of the file.
--Nate
devinemke at devinemke dot com
11-Jan-2002 05:38
If you are using templates with numerous includes then exit() will end you script and your template will not complete (no </table>, </body>, </html> etc...).  Rather than having complex nested conditional logic within your content, just create a "footer.php" file that closes all of your HTML and if you want to exit out of a script just include() the footer before you exit().

for example:

include ('header.php');
blah blah blah
if (!$mysql_connect) {
echo "unable to connect";
include ('footer.php');
exit;
}
blah blah blah
include ('footer.php');

get_browser" width="11" height="7"/> <eval
Last updated: Thu, 31 May 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites