views:

166

answers:

3

How would I go about checking if and include or a require has an error in it. For example, and include would try to be included, if that page has an error the page isn't included and a message is throw?

Cheers.

A: 

You can't trap parser errors.

However, if the code executes something that causes an exception to be thrown, you could catch it with a try/catch block.

jasonbar
+4  A: 

You can't catch a parse error in PHP in the same language environment (for obvious reasons).

One approach might be to run php -l your_included_file.php using exec and then check the exit code. The -l (lint) argument checks that your code can be parsed correctly.

Martin Wickman
A: 

You can try using file_exist function, it check whether the file exist or not.

$filename = "/path/to/file.php";
if(file_exists($filename)){
   include $filename;
}else{
   include "errorpage.php";
}
febfeb