-1

On my WordPress blog, whenever I have a PHP error, it breaks the site and displays a white page with the PHP error.

For example:

Parse error: syntax error, unexpected ';' in...

I can fix the error. However, I don't want users to see a broken site while I do. Is there a setting I can enable that will suppress the error (or more gracefully handle it) and continue to parse the rest of the PHP?

3
  • No, you cannot enabled relaxed parsing. Syntax errors are mostly fatal. Commented Oct 13, 2011 at 17:47
  • 2
    Wouldn't it be better to correct the problem rather than attempt to ignore it? Commented Oct 13, 2011 at 17:47
  • Not trying to ignore it. Just need a bit more graceful warning than "Sorry, I'm stopping everything here!" This particular error was the presense of an extra semi-colon and its completely broken the site. I have to login via FTP to fix it. I was editing the script via WordPress. Commented Oct 13, 2011 at 18:12

3 Answers 3

5

Sure: it's the setting whereby you don't make a parse error in your script. It's a setting in you.

(Parse errors generally can't be recovered from; what meaning does your script have if the parser cannot understand it? If it's not written in valid PHP? What do you want PHP to do?)

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

Comments

-1

When you have a syntax error in a PHP page, it will always immediately stop parsing. It's the same thing as a compile error in languages such as C#, VB etc. There is no getting around this.

There are two ways you can suppress this error message. The first is with the error_reporting directive in your php.ini file. It's likely set to E_ALL & ~E_DEPRECATED. You can fine tune what is reported (via error logging and out to the display) by configuring the directives located on the PHP Site.

The other (simpler) way to suppress these is via the display_errors directive in your php.ini file. Simply set this to Off and restart your web server and you won't see these errors any longer - just a white page.

3 Comments

-1: Did you try suppressing a parse error with either approach, before posting this answer?
hiding errors doesn't fixes scripts. Now he'll be back complaining about "I have a blank page plz help wut can I do?"
@Damien: Not even that (control A and B).
-1

Since PHP 7 and for WP in particular I would suggest to put your your logical parts to separate files and then load them in try catch, because PHP 7 added the \Throwable which can also catch Error including syntax errors when including file - but only from outside the script being included.

So for example in your functions.php you will do include/require/include_once/require_once and put a logic in yourTheme/your_script_with_syntax_errors.php:

functions.php part will look like:

try {
    require_once __DIR__ . '/your_script_with_syntax_errors.php';
} catch (\Throwable $e) {
    // bypassed ...
    // you can put your error handling/logging here
}
echo "Script continues...";

Which in case of a syntax error in the included script will still result in something like:

...
Script continues...

So as you can see, even though there was a syntax error in the included file, the main script still continues...

If you don't want to use an existing logger, you can implement custom error handler and trigger_error in the catch.

set_error_handler(function (int $errno, string $errstr, string $errfile = ?, int $errline = ?, array $errcontext = ?) {
   // log something somewhere...
});

Note that if you don't add an error handler that doesn't print out the message, the error will be output based on your error_reporting level.

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.