« TIBCO General Interface 3.3 Released | Main | DoodleBoard, New AJAX Whiteboard »

How to use Firebug to Debug PHP Scripts

We usually use Firebug to debug Javascript, AJAX and so on, so here is how to use Firebug to debug your PHP scripts. I have written long time ago a PHP class Buggy to help me store debug information in database so it could be better analyzed later. Now that many are using Firebug, I find it pretty interesting to have such debug information available directly in Firebug so you can group both server-side and client-side debug info in a single place.

Last week I saw FirePHP, which introduce a specific protocol to help debugging PHP scripts and add a new tab to FireBug to visualize results. Here is a fast way to create your own debug solution and use Firebug itself to visualize these informations.

The solution is in reality inspired from FirePHP and my old Buggy PHP class, but more simplified to track time consumption in your code :

Usage of the class is simply by setting different $module names to every part that you would like to track. Let say :

And in Firebug you have this message in console :

firebug-php.png

I simply altered the page with some JavaScript, and from PHP you can enable disable debugging message. Now we can add more advanced features for example we can track PHP errors inside Firebug and here is a more complete example :

And below a screenshot of how results looks like in Firebug :

firebug-php-advanced.png

You can also have a look on the link I have provided in the beginning and see how to include SQL queries. Saving debug data in database is very long story that I didn't wanted to talk about here but this will give you lots of ideas to improve your application, you can for example retrieve debug info via AJAX and visualize graphics... etc. Let me know if you have any comment or suggestion.

Bookmark this article at these sites
Comments
1

Great to hear that FirePHP has inspired this solution.

There is another tool similar to the one described in this post at: http://www.appelsiini.net/~tuupola/312/debugging-php-with-firebug

One challenge with both these solutions is that the server response must contain the JavaScript code to insert the messages into the Firebug Console.

FirePHP addresses this issue with the extended protocol and thus allows embedding of debug information in the server response even for binary image requests and pure AJAX JSON or XML calls where the response may not contain the extra JavaScript code.

FirePHP will support inserting messages into the Firebug console and/or a dedicated PHP console in the near future.

2

What a fantastic idea! Very nice job!

3

adding these two functions will allow you two use Buggy:dump($data,'title') to dump arrays and variables. For arrays it uses console.group() for formatting


function dump($data, $name='Data'){

if (is_array($data)){


echo "\n";

echo Buggy::dumpArray($data, $name);

echo "\n";


}else{

echo "console.info(\"[Buggy] - [dump] - "."\$".$name." = ".
addslashes($data)."\")\n";


}

}

function dumpArray($arr,$name,$sublevel=false) {

if($sublevel==false){


$output .= "console.group('Array \$".addslashes($name)."');\n";

}else{

$output .= "console.group('Array [".addslashes($name)."]');\n";


}

foreach($arr AS $key=>$val){

if (is_array($arr[$key])) {


$output .= Buggy::dumpArray($arr[$key],$key,true);

}else{

$output .= "console.log('[".
addslashes($key)."] => ".addslashes($val)."');\n";


}

}

$output .= "console.groupEnd();\n";

return $output;

}



4

I've modifed the dump array function so it works with objects or array, or horrible convoluted combinations thereof.

static function dumpArray($arr,$name,$sublevel=false) {
$output = "";
if($sublevel==false){
$output .= "console.group('Array \$".addslashes($name)."');\n";
}else{
$output .= "console.group('Array [".addslashes($name)."]');\n";
}
foreach($arr AS $key=>$val){
if (is_array($val) || is_object($val)) {
$output .= Buggy::dumpArray($val,$key,true);
}else{
$output .= "console.log('[".addslashes($key)."] => ".addslashes($val)."');\n";
}
}
$output .= "console.groupEnd();\n";
return $output;
}

5

Just something concerning this exemple's OOP :
in Buggy class, why do you use a global $Buggy variable ?
$Buggy belongs to Buggy class so it should be available for any method without specifing its accessibiliy.
If you want this variable to be reachable from any method, make it an object member.

example for object member :

class Buggy {
private $Buggy = array();
}

function SetMicroTime($module)
{
$this->Buggy[$module] = Buggy::getmicrotime();
}
}

example for class member :

class Buggy {
private static $Buggy = array();
}

function SetMicroTime($module)
{
self::Buggy[$module] = Buggy::getmicrotime();
}
}

Post a comment





(Email will remain hidden)





Please enter the security code you see here




Related entries
Email to a friend
Email this article to:


Your email address:


Message (optional):