Detecting failure of include/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
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Detecting failure of include/require

Post by Apollo »

Sometimes I want to require a file, but not show the default PHP error message (with the path and everything) when it fails. But whit an @ in front of it (to suppress error message), the scripts just stops with no error at all.


Is there a way to do something like this:

Code: Select all

@require("somefile.php") or die("File not found :(");
I know I can define some constant in the include, and then in the main file:

Code: Select all

@include("somefile.php");
if (!defined(constant)) die("My favorite error message");
This is what I currently do in such cases.

Is there an even simpler way to react on a failed include or require, that does not depend on custom defines in the included file?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Detecting failure of include/require

Post by RobertGonzalez »

http://us.php.net/include/ Check out example #5
Post Reply