|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-03-18 20:50 UTC] gk at proliberty dot com
According to the manual, require() differs from include() in that failure to open a file is FATAL in require().
Yet, using sapi/cgi/php or sapi/cli/php, fatal errors from require() exit with status=0 instead of non-zero, as one would expect from a 'fatal' error.
I do not know if this problem is true for ALL fatal errors or not.
Calling exit(1) works correctly however, which provides an awkward, but useful, workaround to this problem:
$success=require("non_existent.php");
if (!$success) exit(1);
Here is my test file: /htdocs/common/test/junk/test.php
<?php
$script="non_existent_file";
$success=require_once($script);
//the following fixes the problem:
//if (!$success) exit(1);
?>
[root@p3 sapi]# cli/php -f /htdocs/common/test/junk/test.php; echo status="$?";
Warning: main(non_existent_file) [http://www.php.net/function.main]: failed to open stream: No such file or directory in /usr/local/apache/htdocs/common/test/junk/test.php on line 4
Fatal error: main() [http://www.php.net/function.require]: Failed opening required 'non_existent_file' (include_path='.:/usr/local/apache/htdocs/common/php:/usr/local/lib/php') in /usr/local/apache/htdocs/common/test/junk/test.php on line 4
status=0
[root@p3 sapi]# cli/php -r 'exit(1);'; echo status="$?";
status=1
[root@p3 sapi]#
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 21:00:01 2025 UTC |
Actually I was mistaken about the workaround: THERE IS NO WAY TO EXIT CORRECTLY, USING require(); only with include()! Which is contrary to the entire purpose of using require() instead of include(). This does NOT work: <?php $success=require("non_existent_file"); if (!$success) exit(1); ?> This DOES work: <?php $success=include("non_existent_file"); if (!$success) exit(1); ?>