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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.