OK I will admit this post is purely for me to store this little bit of code for times of need. If you have found this post and want to know what it is about then read on.

How to temporarily enable PHP errors without editing your PHP.ini file (so keeping display_errors = Off globally in your .ini file but changing it to “On” just for one script).

Paste the following code into the top of your PHP file:

If you want to log errors to a particular file you can also add:

Job done.

UPDATE – OK job not completely done. I was well aware that using the above settings would not show fatal errors but that never usually bothered me until I hit a problem with someone else’s code on a server with display_errors Off and didn’t know where to start. PHP will not show fatal errors even using the settings above because if it encounters a fatal error the processing stops dead before the error is triggered and thus there is no output as expected – usually this leaves you with a blank page and a silent 500 internal server error, which is highly frustrating!

To set your site to show fatal errors even when your PHP.ini has display_errors as Off then you can add this to your .htaccess file (using Apache):

That should work as long as your Apache config allows the htaccess file to overwrite all settings.