Over a million developers have joined DZone.

PHP Debug in Browser Console with Xdebug

DZone's Guide to

PHP Debug in Browser Console with Xdebug

Xdebug will allow you to step into function and make sure the variables are being set as the should be.

· Web Dev Zone
Free Resource

Try RAD Studio for FREE!  It’s the fastest way to develop cross-platform Native Apps with flexible Cloud services and broad IoT connectivity. Start Your Trial Today!

When debugging in PHP there are a few techniques you can use, you could use something like Xdebug which will allow you to step through your code at run time and you can see exactly what the code is doing. Xdebug will allow you to step into function and make sure the variables are being set as the should be.

Your other option is to output the code in the browser and exit the script so it just displays what you want to debug and nothing else, something similar to this.

echo '
echo '</pre>

This will allow you to see exactly what is in this variable at a certain time of running your code.

But what if you want to view the rest of the page and debug at the sametime, you can simply print the variable without the exit in your code. But then you get the problem of the print being on the page which could either break your design or the debug will be displayed in the design.

Your other option is to write debug into a log file, this can be done with a debug class which writes to a debug log file, allowing you to view all your variables and not breaking your design.

If you don't want to send your data to a log file you can also try this neat little trick and output debug data into the browser debug console.


Here is a snippet of a function you can use to output PHP data to the browser console.

function debug_to_console($data) {
echo("<script>console.log('PHP: ".implode(',', $data)."');</script>");
} else {
echo("<script>console.log('PHP: ".$data."');</script>");

Get Your Apps to Customers 5X Faster with RAD Studio. Brought to you in partnership with Embarcadero.

php ,html5 ,tips and tricks

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}