pillarOfLight
pillarOfLight

Reputation: 8982

suppress parse errors in PHP

so I'm trying to prevent fatal errors from stopping my script from running

so I set error report to 0:

error_reporting(0);

then I added some junk code afterwards..

junk code~~~~trololololololol

However, PHP ends up returning parse error nonetheless:

Parse error: syntax error, unexpected 'require_once' (T_REQUIRE_ONCE) in etc

is there a way to prevent PHP from ceasing to execute when there are parse errors or is this a hopeless endeavor?

Upvotes: 3

Views: 1705

Answers (4)

Magnus
Magnus

Reputation: 3722

If there is a parse error then php can't execute the script at all, so it won't run the error_reporting(0);, and so it will fall back to whatever the setting was for error_reporting and display_errors in the php.ini file. So you CAN prevent the parsing error being shown by editing the php.ini file, but then that will switch it off globally.

If you want the display of errors to be on by default globally (not good for production!), then you might be able to switch them off locally with a .htaccess setting, since this is read before parsing the php script.

Upvotes: 0

Alexander Yancharuk
Alexander Yancharuk

Reputation: 14501

Yes, you can handle parsing errors. Take a look at this example:

index.php

<?php
ini_set('display_errors', '0');
register_shutdown_function('err_handler');

$modules = array('module1.php', 'module2.php', 'module3.php');

load_modules($modules);

function load_modules(&$modules) {
    foreach ($modules as $key => $module) {
        unset($modules[$key]);
        include $module;
    }
}

function err_handler() {
    global $modules;
    load_modules($modules);
}

module1.php

<?php junk code~~~~trololololololol;

module2.php

<?php junk code~~~~trololololololol;

module3.php

<?php echo "Surprise!\n";

Wisely using the register_shutdown_function(), set_error_handler() and set_exception_handler() you can make your script absolutely immune to all type of PHP errors. This code can be treated as an example of defensive programming technique in PHP.

Upvotes: 0

Carson Myers
Carson Myers

Reputation: 38564

You can't. PHP first reads the text in your script from beginning to end (parses it) and converts it into some form of low-level bytecode that can be executed. If it fails to parse, then there's no executable code at all and it can only fail.

With runtime errors, your code is already executing and PHP already knows if you've got some way to handle that error. But with a parse error, there's no program there to begin with.

Upvotes: 1

Niels Keurentjes
Niels Keurentjes

Reputation: 41958

is there a way to prevent PHP from ceasing to execute when there are parse errors or is this a hopeless endeavor?

Yes, that's hopeless. It's impossible to resume execution after junk was found, there just isn't any predictable state left for the compiler to adhere to. It might be missing random functions, variable definitions and what not, so all it can logically do is stop working.

You can suppress the error but not force it to continue working after the digital equivalent of a violent heart attack.

Upvotes: 3

Related Questions