Wednesday, December 5, 2012

Debugging PHP

Debugging PHP without an IDE (Integrated Development Environment) can be difficult at first. But it's actually pretty easy using the Apache error log. Of course, a full blown IDE will probably offer many more debugging functionalities but for a quick debug the logs can be a great help. This short tutorial assumes you already have a VPS running with Apache and PHP. If you need help to setup a LAMP server, see the previous articles about creating a fully functional VPS webserver:
http://vpstips.blogspot.nl/2010/09/setting-up-lamp-vps-part-1.html
http://vpstips.blogspot.nl/2010/10/setting-up-lamp-vps-part-2.html

To start debugging create a simple PHP script with an error:
echo "<? echo phpinf(); ?>" > /var/www/vps.error.php
This simple script mistakes phpinfo for phpinf (without the letter "o" at the end). This results in a fatal PHP error. The phpinfo functions dipslays some general information about your server and PHP environment. Details: http://php.net/manual/en/function.phpinfo.php

To debug this script look at the Apache error log.
tail -f /var/log/apache2/error.log
Depending on your distribution the log location may vary and you might need root permissions to open the file. The error log will show you an error similar to this one:
PHP Fatal error: Unknown function: phpinf() in...
The error log is a quick way to debug PHP errors. Default PHP installations will supress error messages so if you get a blank page or a half loaded page in your browser it might be worth checking your Apache error logs!

1 comment:

  1. Thanks for providing Vps tips. No doubt this is very useful article on Vps Server. Thanks

    ReplyDelete