开发者

Php - check if an include or block of code has an error

How would I go about checking if and include or a require has an error in it. For exa开发者_Go百科mple, and include would try to be included, if that page has an error the page isn't included and a message is throw?

Cheers.


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.


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";
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜