CartMetrix - Do you know yours?

« ZenCart UPS Shipping Module with Cost Modifier | Home | Installing Payflow Pro SDK on a Shared Hosting Account »

8/22/2005

Debug Variables in PHP

Drop this function into a PHP include and use it to print out variable debugging info during runtime.

<?php
function dv($var, $var_name=, $remote_ip=) {
   if($remote_ip != && $remote_ip != $_SERVER[‘REMOTE_ADDR’])  return;
   $tab = strlen($var_name) > 6 ? \t:\t\t;
   $var_name = $var_name != ? ‘$’ . $var_name . ‘ ‘ . $tab . ‘ = ‘:;
   print(‘<pre> ‘ . $var_name);
   var_dump($var);
   print(“</pre>\n);
} 
?>

To call, use one of the following:

<?php
dv($the_variable, ‘the_variable_name’, ‘192.168.0.1′);
?>

Where:

  • $the_variable - the variable to print
  • the_variable_name - the name of variable to print
  • 192.168.0.1 - your local IP address. Variable will only be echoed when the page is called from this IP address. Useful for debugging on live sites.

The last two parameters are not required but can be used for additional flexibility.

If printing out the same variable at multiple locations in the same page, use successive names for the_variable_name (the_variable_name1, the_variable_name2, etc) to differentiate.

If debugging which file is being processing do:

<?php
dv(__FILE__, ‘file’, ‘192.168.0.1′);
?>

This will echo the filename the function is called from.

If debugging which line the script stops processing on do:

<?php
dv(__LINE__, ‘line’, ‘192.168.0.1′);
?>

This will echo the line number the function is called from.

Popularity: 15%

Trackback:

Related Posts

One Response to “Debug Variables in PHP”

  1. Coding on a Live Site - damonparker.org said:

    […] have written several times about debugging a live site and posted snippets for working on the themes of a live Wordpress install. One trick I haven’t […]

Post your opinion

Verification Image

Please type the letters you see in the picture.

Subscribe without commenting


damonparker.org is proudly powered by WordPress
Entries (RSS) and Comments (RSS).

copyright © 2002-2009 damonparker.org. all rights reserved.

Close
E-mail It