Debugging


Once you realize that an error has occurred in your code, you need to start tracking it down. Apart from the help that error messages and warnings from PHP provide, you need additional assistance to locate and fix the problems. There are two common solutions to this problem.

Instrumenting Your Code

Instrumenting the code to find the source of the bugs is the most common solution used by PHP programmers. By simply inserting echo and other output statements, you can "see" how your program is executing and track down the problem.

Other functions, such as var_dump, gettype, and isset, can tell you what is going on with any given variable at any time:

 <?php   $conn = @new mysqli('host', 'user', 'pwd', 'db');   if (mysqli_connect_errno() !== 0)   {     throw new DatabaseConnException(mysqli_connect_error());   }   $results = @conn->query($query_string);   if ($results === FALSE)   {     throw new DatabaseQueryException($conn->error);   } # DEBUG DEBUG DEBUG -- how many rows we gots? echo $results->num_rows;   while (($row = $results->fetch_assoc()) !== NULL)   { # DEBUG DEBUG DEBUG: look at row contents. var_dump($row); echo "<br/>\n";     if ($row['age'] < 20)     {       add_to_watch_list($row['userid']);     }   }   // etc. ?> 

Debug output clutters up your application significantly, but it can be an extremely useful way to see what is going on. Plus, once you determine what the problem is, you can then remove the statements. Do not forget to remove statements! In the preceding snippet, notice how you have made a point to mark the code so that you can find it later and delete it. Carelessness here can result in some embarrassing output in your final application and even some security holes if you give people too much information about your web application.

You can improve the previous system by writing a DEBUG_OUTPUT function, such as the following:

 <?php define('DEBUG_OUTPUT', TRUE); // // takes N parameters, and just concatenates them all together // function debug_dump() {   if (DEBUG_OUTPUT === TRUE)   {     $args = func_get_args();     $output = '';     foreach ($args as $arg)     {       if (is_array($arg))         $output .=  print_r($arg, TRUE);       else         $output .= $arg;     }   } } ?> 

Now we can change your DEBUG_OUTPUT to look as follows:

 debug_dump('<br/>Number of Rows: ', $result->num_rows); 

We have designed your system in such a way that we could leave this code in the scripts and change the value of DEBUG_OUTPUT in the constant declaration to control whether or not there is any output.

phpinfo is a particularly useful function that not only tells you information about your installation, but also the full contents of the $_SERVER and $_ENV superglobal arrays.

To see the contents of other superglobals, such as $_POST or $_GET, you can write some quick code of your own (or you can use phpinfo):

 <?php   function dump_array($in_array)   {     echo "<table width='100%' border='0'>\n";     foreach ($in_array as $key => $val)     {       echo "<tr><td>$key</td><td>$val</td></tr>\n";     }     echo "</table>\n";   } dump_array($_POST); dump_array($_GET); ?> 

You can get a basic idea of what is going on with your application by using these tools.

Source Code Debuggers

However, source code debuggers, such as those included with Integrated Development Environments (IDEs), are far more robust tools through which you can code, compile, execute, and debug your scripts and applications.

Typically, the commercial ones (for which you must pay) have the most fully featured environments with the most feature-rich graphical debuggers. PHP has developed quite a community of third-party tools to support it because of its popularity, and the following vendors have commercial debugger and IDE products available for PHP:

  • ActiveState Software sells Komodo, a robust development environment for both Linux and Windows that supports other languages in addition to PHP (http://www.activestate.com/Products/Komodo).

  • NuSphere sells PhpED, a complete graphical environment for PHP development for Windows and Linux (http://www.nusphere.com).

  • Waterproof Software sells PHPEdit, a complete development IDE for PHP that runs on Windows systems.

  • Zend Technologies sells Zend Studio, a complete development environment for PHP that runs on Windows and Linux systems.

There are also a few options for debuggers that are free:

  • XDebug, written by Derick Rethans, is a source-level debugger that is freely available for PHP developers. It can be found at http://www.xdebug.org, and it includes a client through which to use it. Both sources and precompiled binaries are available.

  • DBG is a free debugger written by Dimitri Dmitrienko that includes all of the necessary source for a debugger in PHP, in addition to client programs through which to use it. Source can be downloaded at http://dd.cron.ru/dbg.

  • Gubed is another debugger for PHP. More information about it can be obtained and downloaded from http://gubed.sourceforge.net.

All of these products have their various strengths and weaknesses, and it is impossible to give a quick recommendation of one over the others. You are encouraged to spend some time evaluating the various products (including the free ones!) and see which best meets your needs and budgets.

Needless to say, once you begin using source-level debuggers, you will wonder how you ever survived without them!




Core Web Application Development With PHP And MYSQL
Core Web Application Development with PHP and MySQL
ISBN: 0131867164
EAN: 2147483647
Year: 2005
Pages: 255

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net