Debugging PHP scripts
By Justin Silverton
Debugging PHP scripts can be a difficult task, so I have written this simple php debugger class. It will display all GET and POST variables and all the properties on a currently defined object.
The code
(copy this in debug.php)
function DebugClass($class)
{
$class_vars = get_object_vars($class);
echo "<b><u>Class contents</b></u><br><br>";
foreach ($class_vars as $key => $value)
echo "<b>Property Name: </b>".$key." <b>Property Value: </b>".$value."<br>";
}
//this displays all get and post variables
function DebugIncoming()
{
echo "<b><u>GET and POST contents</b></u><br><br>";
echo "<b><font color=\"red\">GET contents</b></font><br><br>";
foreach ($_GET as $key=> $value)
echo "<b>Variable Name: </b>".$key." <b>Variable Value: </b>".$value."<br>";
echo "<br><br>";
echo "<b><font color=\"red\">POST contents</b></font><br><br>";
foreach ($_POST as $key=> $value)
echo "<b>Variable Name: </b>".$key." <b>Variable Value: </b>".$value."<br>";
echo "<br><br>";
}//display all current POST and GET values
DebugIncoming();
Usage
Include the above file from a script you would like to debug. Also, to print out all of the currently defined properties of a class instance, use the following function:
DebugClass($objectInstance);
2 Comments so far
Leave a reply






[…] Sveiki, turbūt visi pastebėjot, kad laikas lekia nerealiai greitai. Jau praėjo visa savaitė po naujųjų linksmybių, o galvos dar tik baigia blaivytis ) Šią savaitę su mumis pasidalino mintimis justjust, parašęs straipsnį “Internet Explorer sąlygos sakiniai - komentarai (if…else)“. Dekui jam. Skaitiniai šiam savaitgaliui: Humble Little Ruby Book (čia visa knyga) Debugging PHP scripts GoogleBot Requested a CSS File! HTML Cheat Sheet (html špera) D Programming Language […]
If you are using Firefox, LiveHTTPHeaders is a useful tool. It’ll show you your GET and POST, as well as all HTTP request/response headers, for every request your browser makes.
Using this, you can also sneak debugging output into your headers (thereby preventing it from affecting your HTML rendering) by doing something like this:
$key = "MyObject";
$message = serialize(get_object_vars($myobj));
header("X-Debug-$key: $message");