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..
echo 'some string'; // I place these throughout the code
echo "<br>start of subroutine_name "; // I use these to see which routines are called
|
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.
echo '<br> $variable1 - '.$variable1; // single quotes stop the variables from being evaluated
echo "<br> \$variable2 - $variable2"; // double quotes allow the variable to be evaluated
// the backslash makes the magic character print normally (\$)
echo "<br> array1['id'] = {$array1['id']}"; // curly braces are required for arrays
echo '<!-- rlc note - '.$_GET['page'].' -->'; // visible in the html but not displayed
// the dot ( . ) means concatenate
|
If you uses double quotes, then variables will be converted and linebreaks (\n) will work.
echo "debug_message - $debug_message \n"; // places a line break in the output
// however <br> displays a line break
|
In various routines, you would add code similar to the following
global $debug_message;
$debug_message .= "<br>$sql"; // concatenate new info to the existing string
|
and display the results via
echo "debug_message - $debug_message";
|
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
object(SimpleXMLElement)#301 (4) { ["title"]=> string(11) "Forty What?" ["from"]=> string(3) "Joe" ["to"]=> string(4) "Jane" ["body"]=> string(57) " I know that's the answer -- but what's the question? " }
|
However, since the actual formatting (in the html file) was
object(SimpleXMLElement)#301 (4) {
["title"]=>
string(11) "Forty What?"
["from"]=>
string(3) "Joe"
["to"]=>
string(4) "Jane"
["body"]=>
string(57) "
I know that's the answer -- but what's the question?
"
}
|
the data can be displayed on more than one line by encapsulating var_dump
in <pre> tags.
$xml = simplexml_load_string($string);
echo '';
var_dump($xml);
echo ' ';
|
As mentioned above, in this case, the echo command did not print anything.
echo $xml; // this shows nothing
|
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.
http://someplace.com/page.php?debug
http://someplace.com/page.php?debug=1
http://someplace.com/page.php?debug&trace
http://someplace.com/page.php?trace
|
To display the trace, place the following in footer.php.
if ($debug_trace){echo " $debug_str";}
|
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.
function mc_generic_function($input_string){
global $debug;
global $debug_str;
$a = "Start of mc_generic_function(\$input_string)\n";
$debug_str .= " $a"; // add entry to trace string
if ($debug) {echo "$a";} // show it right now
// the rest of the function goes here
}
|
Notes:
- \n produces a newline in the html file which helps when reading it
- \$ displays a dollar sign and keeps the value of the variable from being displayed
- $string_variable .= "some string"; // adds the string to the existing value
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