Error Trapping with include and require

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Error Trapping with include and require

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

<?php
$i = @include('not_a_file.php');
echo ($i===FALSE) ? 'error' : 'success';
?>
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Weird results.

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Much Thanks

Post by EricS »

Makes total sense. :oops:

Much thanks Volka
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply