1

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.

CafeHey
  • 5,699
  • 19
  • 82
  • 145

3 Answers3

5

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
  • 19,662
  • 12
  • 82
  • 106
1

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
  • 11
  • 1
0

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
  • 13,333
  • 4
  • 38
  • 46