A simple way to debug a php application in production mode

Useful php debug function to output data with line number

A simple way to debug a php application in production mode

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:

if(!function_exists('pr')) {<br /> function pr($p, $func="print_r",$r=false) {<br /> if(defined('DEBUG_REMOTE_ADDR') &amp;&amp; $_SERVER['REMOTE_ADDR'] != DEBUG_REMOTE_ADDR) return;<br /> if(!function_exists($func)) {<br /> die("Debug function {$func} does not exist!");<br /> }<br /> if(!$func) $func='print_r';<br /> $bt = debug_backtrace();<br /> $caller = array_shift($bt);<br /> $file_line = "&lt;strong&gt;" . $caller['file'] . "(line " . $caller['line'] . ")&lt;/strong&gt;\n";<br /> if(!$r) { //if print<br /> echo '&lt;pre&gt;';<br /> echo '&lt;!--Debugger Line: ' . $file_line . '--&gt;' . $dt;<br /> print_r($file_line);<br /> $func($p);<br /> echo '&lt;/pre&gt;';<br /> } else { //if return<br /> ob_start();<br /> echo '&lt;pre&gt;';<br /> print_r($file_line);<br /> $func($p);<br /> echo '&lt;pre&gt;';<br /> $d = ob_get_contents();<br /> ob_end_clean();<br /> if(filter_var($r, FILTER_VALIDATE_EMAIL)) {<br /> $headers = 'From: webmaster@example.com' . "\r\n" .<br /> 'Reply-To: webmaster@example.com' . "\r\n" .<br /> 'X-Mailer: PHP/' . phpversion();<br /> mail($r, 'Debug Output', $d, $headers);<br /> }<br /> return $d;<br /> }<br /> }<br />}

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

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>