Since PHP code will run just fine even if it's riddled with warnings and notices about undefined indexes and non-static methods being called as static, etc, the question is, if I spend the time to remove ALL notices and warnings from my code will it run significantly faster?
Remember, the PHP ini configuration has an error_reporting directive that will be set by this function during runtime. error_reporting(0); To remove all errors, warnings, parse messages, and notices, the parameter that should be passed to the error_reporting function is zero.
A warning error in PHP does not stop the script from running. It only warns you that there is a problem, one that is likely to cause bigger issues in the future. The most common causes of warning errors are: Calling on an external file that does not exist in the directory. Wrong parameters in a function.
I bookmarked an article in which the author did some benchmarks about that ; unfortunatly, it's in french... but here it is (maybe you'll understand some parts of it) : Ne faites pas d'erreur
And here are the numbers, to help people who don't read french :
error_reporting
and display_errors
enabled : 5,162.76 msdisplay_errors
disabled : 136.18 mserror_reporting
disabled too : 117.79 msWhich means that, yes, PHP code runs faster without notices/warnings/errors, even when those are not displayed nor reported.
Derick Rethans says the same thing in this article : Five reasons why the shut-op operator (@) should be avoided (quoting) :
Reason 3: It's slow (part 2)
Whenever PHP generates an error message internally, it's processed and formatted all the way up to the fully formatted message that can be outputted straight to the browser.
Only just before it is displayed theerror_reporting
setting is checked. This however, is not related to the @-operator exclusively.
The error message is just always fully formatted beforeerror_reporting
is checked—ordisplay_errors
for that matter.
Depends on the amount of warnings, but the error handling in PHP is, even when hiding error messages, relatively expensive.
What you'd have to do to estimate the effect is profiling on C level: Install valgrind (assuming you're on Linux) and then run
callgrind /path/to/bin/php /path/to/script.php
this generates a file called callgrind.12345
or so, load this file into an app like kcachegrind and look for php_error_docref0
or php_error_cb
to see how much time was spent in the error handler.
Please mind the cachegrind and valgrind docs when doing that and mind that there are many system-dependant variables involved.
EDIT: Oh one more note: I assume that way more time is spent while talking to databases and similar systems. and yet another additional note: fixing notices usually makes the code more robust for future changes so it's a good idea independent from performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With