- Site Check – Web Site Advice - https://www.sitecheck.be -

PHP debugging

Sometimes there are little errors in your PHP scripts and you need to debug them. If you don’t have access to the global PHP error log, then it’ll be difficult to see what goes wrong and where.

However if you’re able to put a .htaccess file on your web host, then you can add some little commands to that file to enable PHP logging and displaying. This allows you to find out what’s happening in your script.

Displaying php errors in the browser window:

Put the following code into your .htaccess file:

php_flag display_errors on

Attention: this command will show all php errors in your browser window and other people will see these errors too, when they browse your site. The php errors display the name and path of the files where the error occured (also included files). You should only enable this feature when you really need it. Make sure to disable it afterwards.

Logging php errors to a file

php_flag log_errors on
php_value error_log php.txt

You should replace php.txt with the correct path to your log file. That file needs to be writable by the owner under which PHP runs. Usually this is nobody, apache or www when you’re running PHP as an Apache module. Make sure the directory that contains the log file has the right permissions.

Here’s the complete .htaccess file

php_flag display_errors on
php_flag log_errors on
php_value error_log php.txt

Another handy way for debugging PHP scripts is to use echo $variable whenever you suspect that there is something going wrong in your script. Just put this command after all important steps in your script and display the value of your important variables.

Happy debugging!

Some more debugging links: