Languages - PHP
Debug Tips

The simplest way to debug PHP scripts is to use something like Eclipse.

This covers other techniques for those without Eclipse.


echo - Display text and variables

The primary technique is to simply output some text that says "I am here" - it just indicates where the code is executing.. It is also useful to see what the values of some variables are. Because these are displayed in the web page, adding spaces or a little html will make the output easier to read. If you uses double quotes, then variables will be converted and linebreaks (\n) will work. In various routines, you would add code similar to the following and display the results via

For those messages that flash by too fast to read, try Ctrl-Shift-PrtSc (to capture a screen image) and then paste into Windows Paint (under Accessories/Paint.)


var_dump

Sometimes, particularly with complex data types, echo won't display anything. In these cases, try var_dump. Using the simplexml example, var_dump displayed However, since the actual formatting (in the html file) was the data can be displayed on more than one line by encapsulating var_dump in <pre> tags. As mentioned above, in this case, the echo command did not print anything.


Program Trace

Rather than adding and removing lots of echo commands, I prefer to design functions with built-in, program controlled, debug support. This way, with a complex application, it is much quicker to locate problems. As shown, the idea is to set the 3 debug parameters based on a command line (url) parameters. To display the trace, place the following in footer.php. In a production environment, it is a good idea to disable these features. I suggest just using another debug_constants.php file where the variables are not changed.


Functions

This is one way to instrument functions and use the variables defined in the previous section. Notes:


Queries

Since many web sites have a database backend, a significant number of problems are related to building queries. I have a separate page that discusses queries and suggests how to instrument them using the ideas presented above. The $debug_query flag provides an additional level of control.


Author: Robert Clemenzi
URL: http:// mc-computing.com / Languages / PHP / Debug.html