Useful php debug function to output data with line number

A very useful php debug function i like to use to print debug output to browser window. In some cases where i may not output the debug to browser window i use the same php debug function to receive debug output in my email inbox.

We usually use print_r or var_dump to show debug output. This php debug function uses print_r by default but one can use var_dump to show the output. This function was created to show the print_r output enclosed in <pre> tags. Later it was extended to show line number where print output was sought. Later var_dump and email options were added. This php debug function has proved very good tool for me to debug php applications. I hope someone else might find it useful, thus sharing.

Here’s the function:

[php]if(!function_exists(‘pr’)) {
function pr($p, $func="print_r") {
if(defined(‘DEBUG_REMOTE_ADDR’) && $_SERVER[‘REMOTE_ADDR’] != DEBUG_REMOTE_ADDR) return;
$bt = debug_backtrace();
$caller = array_shift($bt);
$file_line = "<strong>" . $caller[‘file’] . "(line " . $caller[‘line’] . ")</strong>\n";
echo ‘<pre><!–Debugger Line: ‘ . $file_line . ‘–>’ . $dt;print_r($file_line);$func($p).'</pre>’;
}
}[/php]

How to use this php debug function:

To print simple output, where is $output is the output to be printed.

  • pr($output) ; //uses print_r by default enclosed in <pre> tags to print output
  • pr($output, “var_dump”); //prints output using var_dump enclosed in <pre> tags
  • pr($output, ”, true); //returns output ; uses print_r enclosed in <pre> tags
  • pr($output, “var_dump”, ‘your@domain.com’); //sends var_dump output enclosed in <pre> tags to email address supplied. Returns as well.
  • pr($output, ”, ‘your@domain.com’); //sends print_r output enclosed in <pre> tags to email address supplied. Returns as well.

One thought on “Useful php debug function to output data with line number

Leave a Reply