Main Content

Debugging multipage (session based) PHP applications

Archive - Originally posted on "The Horse's Mouth" - 2009-07-09 07:56:17 - Graham Ellis

PHP's print_r function allows you to dump out the contents of a variable - and that includes more complex variables like arrays containing other arrays, objects, etc ... in a human (programmer!) readable form. By default, the output string is directly sent to the browser, but with a second parameter that's a true value, it is returned to you for further processing.

Here's an example of print_r's output being decorated to allow for special characters and to retain the spacing so that it can be displayed via the browser:

<?php print ("<pre>".htmlspecialchars(print_r($_SESSION,1))."</pre>"); ?>

And here is a sample of that running in our session demonstration:


(run the demonstration here with that extra trace line now added; you'll find that our wrapper is maintaining your session history too, but that's another story!)

This is a particularly useful facility to use if you're debugging a multipage application and want to see how the data is passed from one page to another.

Remember - if you alter the PHP script that's running a multipage application, you'll probably need to delete the session cookie too ... otherwise you will have changed the program but your testing would continue with the old data!