3

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?

3
  • The best thing to do is fix the errors. Is there some reason you need to handle them? Commented Dec 11, 2013 at 0:55
  • basically the site is divided into modules, I want to be able to run a module nicely even if there is a parse error in another separate module...but a single parse error from one module would stop all modules from running nicely even if the other modules dont have parse errors Commented Dec 11, 2013 at 0:57
  • You can check a file's syntax with system('php -l filename') (info) but that's slow. Essentially, if a module is broken, you can catch it with register_shutdown_function and you should automatically disable it. It's like loading DLLs in a Windows program - if they segfault, the program as a whole is unpredictable and has to halt, but you can still take evasive action to stop it loading the same DLL the next time it loads. Commented Dec 11, 2013 at 1:09

4 Answers 4

3

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.

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

Comments

1

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.

Comments

0

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.

1 Comment

@pillarOfLight Isn't this the answer on your question?
0

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.

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.