Page 1 of 1

Error Trapping with include and require

Posted: Tue Jan 21, 2003 11:54 am
by EricS
I'm wanting to trap and handle my own errors. The problem I'm running into is with require and include. It appears that it doesn't return anything. Is there anyway to check to see if a file has been included? I know I can use @ to suppress the php fatal error, but I need to know how to evalute it's inclusion so I can handle the error myself.

Posted: Tue Jan 21, 2003 12:56 pm
by volka
try

Code: Select all

<?php
$i = @include('not_a_file.php');
echo ($i===FALSE) ? 'error' : 'success';
?>

Weird results.

Posted: Tue Jan 21, 2003 1:38 pm
by EricS
Thanks Volka,

I tried your code and got weird results, so I tested it further to try and nail down the problem.

It appears that when I use @require_once(), if a fatal_error occurs, PHP will suppress the error, but I can't send anything to the output buffer after that. It appears to just end the script and outputs nothing.

Any idea what's happening. Im running 4.0.6 in cgi mode.

Thanks

Posted: Tue Jan 21, 2003 2:11 pm
by volka
if you use require(_once) than the file is ..well.. required, mandatory. ;)
Without the script can't continue. It's more or less like include(...) or die(...)
use include or include_once to trap errors.

Much Thanks

Posted: Tue Jan 21, 2003 2:47 pm
by EricS
Makes total sense. :oops:

Much thanks Volka

Posted: Wed Jan 22, 2003 2:44 am
by twigletmac
Another approach could be to test to see if a file exists (using file_exists()) if it doesn't then don't try and include it.

Mac