Two things I do to make my life easier when trying to track down bugs in PHP:
First, assuming display_errors is on,
adding this to php.ini helps to ensure that you can actually read
error messages when they get spit out in the middle of some markup
that would otherwise cause the browser to obscure them in
the rendered content, forcing you to view the HTML
source to figure out what the problem is:
error_prepend_string = "<div style='background: #fff;
padding: 25px; color: #000; border: solid 10px #f00;
width: 500px; clear: both'>"
error_append_string = "</div>"
If you don't have access to php.ini, those parameters can be set in httpd.conf, (or .htaccess, if AllowOverride is set to 'Options' or 'All') like this:
php_value error_prepend_string "<div style='background: \
#fff; padding: 25px; color: #000; border: solid 10px \
#f00; width: 500px; clear: both'>"
php_value error_append_string "</div>"
Second, I include this small function for every project:
function trap ( $var = NULL, $exit = 1 )
{
if ( isset ( $var ) )
{
echo '<pre>';
print_r ( $var );
echo '</pre>';
if ( $exit ) exit;
}
else
{
echo '<span style="font: 14px bold
sans-serif; color: red">***</span>';
}
}
It allows me to quickly & easily determing the content of a
variable (including arrays) by adding trap($foo)
to the script. Omitting the variable name prints red asterisks
wherever the function is used.
Any other tips, tricks, or gotchas?