Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

suppress parse errors in PHP

Tags:

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?

like image 603
pillarOfLight Avatar asked Aug 31 '26 07:08

pillarOfLight


2 Answers

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.

like image 72
Niels Keurentjes Avatar answered Sep 02 '26 23:09

Niels Keurentjes


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.

like image 42
Carson Myers Avatar answered Sep 03 '26 00:09

Carson Myers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!