php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22775 Fatal error from require() exits with status=0
Submitted: 2003-03-18 20:50 UTC Modified: 2003-03-19 02:23 UTC
From: gk at proliberty dot com Assigned:
Status: Closed Package: CGI/CLI related
PHP Version: 4.3.2RC1 OS: linux RH 7.2; kernel 2.4.18
Private report: No CVE-ID:
 [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]#

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-18 21:46 UTC] gk at proliberty dot com
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);
?>
 [2003-03-19 01:40 UTC] gk at proliberty dot com
improved the title
 [2003-03-19 02:23 UTC] sniper@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sat Apr 19 04:01:55 2014 UTC