Home > Articles > Programming > PHP

  • Print
  • + Share This
This chapter is from the book

3.10 Debugging PHP

After you have learned to write simple PHP applications, it is time to verify that your applications work. Therefore debugging has to be done.

In PHP3 a debugger was included that was able to send data to a TCP port where it can easily be monitored with the help of a listener.

Unfortunately in PHP this debugger can only be used if you buy the ZEND IDE (http://www.zend.com/store/). Some other debuggers are available for PHP, but we do not cover these products in this book. However, in this section you learn to debug PHP programs using PHP onboard tools.

The easiest way to debug PHP code is to write a debugging function. Let's assume that you want to monitor the content of $i:

    include ("debug.php");
    for   ($i=1; $i <= 5; $i++)
        echo "i: $i<br>\n";
        debug("\$i: $i");

Every time the loop is processed, you can call a function called debug, which can be found in debug.php.

Let's see how we implemented this function:

# Write debugging info to a logfile
function debug($var)
    $datum=strftime("[%d/%b/%Y:%T %Z]");
    $log_str="$datum -- $var\n";
    if ($fp)

First, the way time is displayed is set to English style. In the next step the date is compiled and stored in $datum. $log_str will contain the complete string added to the logfile, which will be located in the /tmp directory.

With the help of fwrite, $log_str is added to the file before it is closed again.

After executing the script, you can find the debugging information in the logfile:

[27/Oct/2001:09:38:31 CEST] -- $i: 1
[27/Oct/2001:09:38:31 CEST] -- $i: 2
[27/Oct/2001:09:38:31 CEST] -- $i: 3
[27/Oct/2001:09:38:31 CEST] -- $i: 4
[27/Oct/2001:09:38:31 CEST] -- $i: 5

As you can see, the logfile is generated using the Apache style of logging. With the help of logfiles, it is an easy task to monitor your application and find out what has happened inside a program.

However, this information might not be enough for you. Especially when monitoring production environments in which many concurrent processes are running on the system, it is necessary to have some information about which process generated what kind of information. To compute this information, PHP offers some easy-to-use functions. getmypid retrieves the process ID of the current process and get_current_user finds out which user executes the script.

The information can be added to the logfile by modifying the line generating $log_str:

$log_str="$datum [".getmypid()."] ".get_current_user()." -- $var\n"; 

Now the logfile will contain the process ID and the name of the user executing the script:

[27/Oct/2001:10:06:11 CEST] [4661] hs -- $i: 1
[27/Oct/2001:10:06:11 CEST] [4661] hs -- $i: 2
[27/Oct/2001:10:06:11 CEST] [4661] hs -- $i: 3
[27/Oct/2001:10:06:11 CEST] [4661] hs -- $i: 4
[27/Oct/2001:10:06:11 CEST] [4661] hs -- $i: 5

With the help of this information, it is an easy task to distinguish the scripts running on the server.

In general it is hard to give rules for how debugging can be done best. In most cases, how to find bugs fast and reliably is a matter of personal preference. One thing can be said for sure: Don't hurry when looking for bugs.

  • + Share This
  • 🔖 Save To Your Account