How to catch error from include "file.php"?

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
ericm
Forum Newbie
Posts: 17
Joined: Tue Aug 26, 2008 8:33 am

How to catch error from include "file.php"?

Post by ericm »

I have a simple template system that uses extract, ob_start, include, and ob_get_contents. I've ran into several instances where errors in the template has resulted in a blank page being display. This is, of course, unless I remove the error suppressor "@" from the include line, which gives me the parse error message. What I would prefer is to hide the specific error, but catch it and throw my own custom error message. Unfortunately I haven't figured out just how to do that. I've tried...

Code: Select all

if(!@include("file.php")) {...}
...and...

Code: Select all

if((@include "file.php") === false) {...}
Is there a way to still use @include and somehow catch the error? I think error_get_last() is the answer, but I first need to figure out how to get @include to trigger false.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: How to catch error from include "file.php"?

Post by yacahuma »

there is no reason to have errors in your includes. You should fix them and stop using the @include the @ is good for some very specific calls(like curl, and some file system stuff) not for includes. IMHO
Post Reply