Debugging PHP Web apps is hard to do

Don't get caught without these handy tools for straightening out your HTML, JavaScript, PHP, and SQL code

Debugging a PHP application if often more difficult than debugging an application in some other programming language. There are two reasons for this.

First, a PHP application rarely consists of only PHP code. (Note that we are discussing here the typical use of PHP in a Web application. These issues don't appear in PHP applications executing as command-line scripts.) The application is invariably a mixture of PHP, HTML, CSS, JavaScript, and SQL. The application's execution moves in and out of each language's context; the various languages have different regions of execution. HTML and JavaScript run in the browser, PHP executes in the Web application server, and SQL executes in the database server.

[ PHP developers have many good IDEs to choose from. See the InfoWorld Test Center's comparison of ActiveState Komodo, CodeLobster, Eclipse PDT, phpDesigner, NetBeans, NuSphere PhpED, PHPEdit, and Zend Studio. ]

Second, a PHP Web application does not have a continuous, linear flow of execution. Its execution is a series of HTTP request/response transactions. Each transaction is independent of all other preceding and following request/response pairs. While it is true that, from a user's perspective, the application gives the impression of a monolithic unity, that effect is maintained by artifacts such as cookies or hidden fields. The presence of such artifacts does not inform the debugger of a transaction-spanning Web session.

The upshot is that, if you start a debug session on file X.PHP, when X.PHP sends its HTTP response, that PHP code is completed. A subsequent HTTP request in the same user session might trigger the execution of file Y.PHP. Consequently, a single user session effectively passes through multiple debug sessions.

Developers can deal with the "multiple languages" issue using a collection of debug tools, each outfitted for its particular domain. A PHP debugger "extension" handles debugging PHP code. One of the most popular extensions is the Xdebug, a free PHP debugger, which is installed as a dynamic link library on Windows or as a shared library on Linux. Xdebug can be used with virtually any Web server, because it is attached to the PHP runtime and not the Web server.

Xdebug provides all the basic debugging capabilities: breakpoints, stack traces, variable watches, and so on. It also makes special functions available to the executing program. For example, you can display the amount of memory a PHP application is using by calling Xdebug's xdebug_memory_usage() function. Finally, Xdebug can be configured to track an HTTP session. Appending an XDEBUG_SESSION_START parameter to the URL of an HTTP request will cause Xdebug to emit a cookie to the browser, thus enabling a single debug session to span multiple HTTP transactions.

HTML and JavaScript debugging can be handled by the excellent Firebug plug-in (also free) for the Firefox browser. Because Firebug runs in the browser, it can be executed independently of a PHP IDE, though some IDEs actually integrate with Firebug. The NetBeans IDE provides its own JavaScript debugging plug-in, which is automatically installed in Firefox when a debug session is started that involves JavaScript.

Debugging SQL is different from debugging a procedural language. Testing the correctness of a query is often done simply by building and executing the query in ad hoc fashion -- that is, typing the query into an SQL management console and verifying the results. Many PHP IDEs provide database tools for this very reason. For example, the Database Development Toolkit in Eclipse (and Zend Studio) includes ad hoc query execution, as well as execution statistics. With the proper plug-in (dependent on the database engine), you can also display query plan information.

Whichever PHP IDE you choose, examine its debugging capabilities in the light of your proposed application. If your application will rely lightly on JavaScript, then your requirements for JavaScript debugging will be minimal. In any case, you do not want to find yourself in the middle of a tense PHP debugging session without the necessary tools.

This article, "Debugging PHP Web apps is hard to do," was originally published at InfoWorld.com. Follow the latest news in software development and PHP at InfoWorld.com.

Copyright © 2010 IDG Communications, Inc.

InfoWorld Technology of the Year Awards 2023. Now open for entries!