Section 22.1. The Most Basic Debugging Technique


22.1. The Most Basic Debugging Technique

If you are experiencing a problem with your script, the time-honored way to figure out what's going on is to sprinkle your code with lots of print statements. This is a technique that few people will admit they use, but I can assure you it is widespreadand not just in the PHP programming world! Consider this following script:

     $foo = "bar";     $wombat = somefunc($foo);     print "After somefunc( )\n";     $wombat2 = somefun2($wombat);     print "After somefunc2( )\n"; 

If we found that somefunc2( ) was causing a problem that caused PHP to silently exit the script, we would see the output "Aftersomefunc( )", but not "Aftersomefunc2( )", which points to the problem function.

This method has benefits: it is easy to use, and will generally find the problem through trial and error. The downsides are clear, though: you need to edit your script quite heavily to make use of the print statements, then you need to re-edit it once you have found the problem to take the print statements back out. Furthermore, the technique is a relatively slow way of finding problems, as you literally need to keep placing more and more print statements until you find the problem.

Many people combine this with the use of var_dump( ) to inspect variable contents at various points in their script. If you do not have a good debugger (such as the one built into the Zend Studio IDE), this is the only way you will find out what your variables contain; however, you may find it easier to use the function debug_zval_dump( ), which takes one parameter (the variable to dump information about) and prints out even more detailed information than var_dump( ). The key advantage of debug_zval_dump( ) is that it prints out the refcount value of variables sent into itthat is, how many times each variable is being used. If you have trouble getting references to work, using debug_zval_dump( ) is a smart move.

For more advanced debug output, use the debug_backtrace( ) function discussed in the section "See Backtracing Your Code" later in this chapter.



PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

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