RegEdit
RegEdit

Reputation: 2162

How to bypass a syntax error in a PHP script?

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?

Upvotes: -1

Views: 4142

Answers (3)

jave.web
jave.web

Reputation: 15032

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.

Upvotes: -1

Chris Weldon
Chris Weldon

Reputation: 97

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.

Upvotes: -1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

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?)

Upvotes: 5

Related Questions