PHP: Force Errors to display to Screen in Script

PHPMore often than not when you are working in a hosted environment that you yourself don’t run, the webserver that you are uploading your scripts to will have the php.ini file set so that all errors are captured in log files instead of going to the screen. Obviously this is far more secure than printing out everything to the screen, but there are certainly times when this can become quite a headache and you actually wish to just see what the heck it is that keeps bombing your script at runtime.

The solution to this problem is to overwrite the php.ini directives by placing the following block of code at the top of your script:

// Report all PHP errors
ini_set(‘display_errors’, 1);
error_reporting(E_ALL);

If per chance you rather want to send all the errors to a file whose path you do know, you can also set something like:

ini_set(‘log_errors’, 1);
ini_set(‘error_log’, dirname(__FILE__) . ‘/error_log.txt’);

This allows you to overcome the security-imposed limitation on a script by script basis, something which has proven to be quite useful over the years! :)

You might also enjoy:

About Craig Lotter

Craig Lotter is an established web developer and application programmer, with strong creative urges (which keep bursting out at the most inopportune moments) and a seemingly insatiable need to love all things animated. Living in the beautiful coastal town of Gordon's Bay in South Africa, he games, develops, takes in animated fare, trains under the Funakoshi karate style and for the most part, simply enjoys life with his amazing wife and daughter. Oh, and he draws ever now and then too.
This entry was posted in Technology & Code, Tutorials. Bookmark the permalink.
    blog comments powered by Disqus