PHP – Function to Format Debugging Messages

<?php
function debug($line, $msg){
   static $calls = 1;
   print "<P><HR><br>\n";
   print "DEBUG $calls: Line $line: $msg<br>";
   $args = func_get_args();

   if(count($args) % 2){
     print "Odd number of args<BR>";
   }
   else{
     for($x=2; $x<count($args); $x+=2){
        print "&nbsp&nbsp; \$$args[$x]: ".$args[$x+1];
        print " .... (".gettype($args[$x+1]).")<BR>\n";
     }
  }
  print "<hr><p></p>\n";
  $calls++;
}
$test = 44;
debug(__LINE__, "First message", "test", $test);
$test = 88;
$test2 = $test/2;
debug(__LINE__, "Second message", "test", $test, "test2", $test2);
?>

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail

Leave a Comment

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