Sending "var_dump" to FireBug console

PHPst picture PHPst · Feb 21, 2013 · Viewed 17.2k times · Source

As you know var_dump() in addition to value show its data type and length.

Is there any way to log its output to FireBug console?

I tried FirePHP and FireLogger but both output only value of a variable (sometimes even incorrect variable value).

Answer

Mark picture Mark · Feb 21, 2013

You can dump JavaScript to the console by putting a console.log() in a script tag:

<script type="text/javascript">
console.log("hello");
</script>

So if you do a php dump in there...

<script type="text/javascript">
console.log("<?php var_dump('abc'); ?>");
</script>

You just need to be careful about ' and " in the var_dump breaking your JavaScript. In this example it will be ok because the HTML would be:

<script type="text/javascript">
console.log("string 'abc' (length=3)");
</script>

Just remember the php is processed then put in the JavaScript. You could also dump it to a comment:

<!--
<?php 
var_dump('abc');
?>
-->

Then you can view source, or inspect element.