Take the 2-minute tour ×

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).

share|improve this question
have you tried any Debugger ? like xdebug ? –  Sujit Singh Feb 21 '13 at 5:38
@SujitSingh I want the output be shown on console, when you are developing an MVC application you can not see output of xdebug when it is called from controllers or models. –  PHPst Feb 21 '13 at 5:40
updated my answer –  RRikesh Feb 21 '13 at 6:12
For me, it's kind of "putting stuff inside what they can't belong". Firebug aims to handle what happens on the client side: css, html, js, event, xhr, etc.. not on the server side. Usually good framework, give ability to see what going on on their side when it builds the page. Symfony, Django, ZF, etc .. have a web debug toolbar for that. It's kind of the firebug for server side. What I recommend you to do instead of have a one place to see the rendering and the server side debugging, it too put var_dump() (maybe var_export(..., true) could be better) inside a log file and tail this file –  j0k Mar 3 '13 at 16:04
I think you should be more concrete why FirePHP or FireLogger doesn't do the job for you because those two tools are exactly for that case. –  hakre Mar 7 '13 at 16:48
show 6 more comments

13 Answers

up vote 6 down vote accepted

Maybe what you need is something like this:

function var2console($var, $name='', $now=false)
   if ($var === null)          $type = 'NULL';
   else if (is_bool    ($var)) $type = 'BOOL';
   else if (is_string  ($var)) $type = 'STRING['.strlen($var).']';
   else if (is_int     ($var)) $type = 'INT';
   else if (is_float   ($var)) $type = 'FLOAT';
   else if (is_array   ($var)) $type = 'ARRAY['.count($var).']';
   else if (is_object  ($var)) $type = 'OBJECT';
   else if (is_resource($var)) $type = 'RESOURCE';
   else                        $type = '???';
   if (strlen($name)) {
      str2console("$type $name = ".var_export($var, true).';', $now);
   } else {
      str2console("$type = "      .var_export($var, true).';', $now);

function str2console($str, $now=false)
   if ($now) {
      echo "<script type='text/javascript'>\n";
      echo "//<![CDATA[\n";
      echo "console.log(", json_encode($str), ");\n";
      echo "//]]>\n";
      echo "</script>";
   } else {
      register_shutdown_function('str2console', $str, true);

Usage: var2console($myvar, '$myvar'); or simply var2console($myvar);

It should very rarely be necessary to set the $now parameter to true, causing the immediate output of the <script> tag. The advantage of using register_shutdown_function() is that you don't need to pay attention to "where you are" in the HTML.

The json_encode() preserves all characters in the transfer from PHP to JavaScript. The only caveat is about encoding: json_encode() only works with UTF-8 (which is the recommended encoding in most cases, anyway). You may need something like utf8_encode() or mb_convert_encoding() if you use a different encoding (or rather, you may consider switching to UTF-8).

The output to Firebug's console is simply the output of var_export(), preceded by the type of the variable, including the length of strings and the count of arrays, and, optionally, by the name of the variable.

var_export() provides a more readable output than var_dump(). If you really need the output of var_dump(), you can use something like this:

function dump2console($var, $name='', $now=false)
   if (strlen($name)) {
      echo "$name =\n";
   $str = ob_get_clean();
   str2console($str, $now);

Usage: dump2console($myvar, '$myvar'); or simply dump2console($myvar);

You should avoid circular references (var_dump() detects them a step too late, and var_export() doesn't detect them at all). This is how to do it, e.g., for $GLOBALS:

function globals2console($now=false)
   $g = $GLOBALS;
   $g['GLOBALS'] = '(recursion)';
   var2console($g, '$GLOBALS', $now);
share|improve this answer
Why did you not use gettype() for determining variable type easily? –  PHPst Mar 12 '13 at 19:44
@PHPst only in order to be fully in control. I prefer bool over boolean, float over double (and, who knows, maybe you prefer int over integer) - and furthermore I already need the ifs in order to add the length of strings and the count of arrays. –  Walter Tross Mar 13 '13 at 7:55
add comment

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

<script type="text/javascript">

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

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

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)");

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


Then you can view source, or inspect element.

share|improve this answer
This is not practical in MVC –  PHPst Mar 4 '13 at 17:57
Then another good question might be how to make this practical in MVC. This is fine for testing and getting values but if you want to use it in production you should probably look at covering your php to json so you can handle it properly in JavaScript. –  Mark Mar 5 '13 at 6:23
I will upvote your answer. The exact to what I was going to write. –  KarmicDice Mar 5 '13 at 11:11
in my PHP var_dump('abc') is string(3) "abc", which already breaks the code, but any array will break it too, because of the newlines. –  Walter Tross Mar 10 '13 at 1:45
add comment

FirePHP does the job well + you can use it while you're developing Ajax.

Sample code:

require_once('FirePHPCore/fb.php'); # add the library

fb($var); #log the variable
fb( var_export($var,true) ); # log the variable with the var_export format

If you're passing an array to it, you can click it from your console and it will open a popup on your screen. You can even expand/collapse arrays and objects.

EDIT: If you're looking for data types and length, use var_dump().

fb( var_dump( array(
  ) ) );
share|improve this answer
Did you test your answer before posting it? fb( var_export($var,true) ); only echoes value! –  PHPst Feb 21 '13 at 9:25
Yes, it echoes the value in the browser console. if you want it to be clickable and pop up, you should pass it as an array fb( array( var_export($var,true) ), 'TEST TITLE' ); –  RRikesh Feb 21 '13 at 9:39
Outputs of fb($var);and fb( var_export($var,true) ); are identical for me. –  PHPst Mar 10 '13 at 14:10
add comment
<script>console.log( <?= json_encode( $var ) ?> )</script>

Just throwing my hat in the ring. It sounds like FirePHP is the best way to go.

share|improve this answer
This is an excellent answer. Note that <?= is shorthand for <?php echo I don't have this enabled, so what works for me here is: <script>console.log( <?php echo json_encode( $var ) ?> )</script> –  iPadDeveloper2011 Oct 8 '13 at 1:12
add comment

I always use this script in combination with Zend_Log_Writer_Firebug (using firephp http://www.firephp.org/), because after redirects in the applicaton or ajax requests debugging with xdebug does not always work as expected:

require_once '/Zend/Log.php';
require_once '/Zend/Log/Writer/Firebug.php';  
require_once '/Zend/Controller/Response/Http.php';
require_once '/Zend/Controller/Request/Http.php';

// create the logger and log writer
$writer = new Zend_Log_Writer_Firebug();
$logger = new Zend_Log($writer);

// get the wildfire channel
$channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();

// create and set the HTTP response
$response = new Zend_Controller_Response_Http();

// create and set the HTTP request
$channel->setRequest(new Zend_Controller_Request_Http());

// record log messages

// insert the wildfire headers into the HTTP response

// send the HTTP response headers

You can build your own library to get the type of a variable:

function get_type($var) 
        return get_class($var);
        return 'null';
        return 'string';
        return 'array';
        return 'integer';
        return 'boolean';
        return 'float';
        return 'resource';
    //throw new NotImplementedException();
    return 'unknown';

Using a function call to var_dump_ret as argument for $logger->info() might be helpful, too. I haven't tested it yet.

function var_dump_ret($mixed = null) {
  $content = ob_get_contents();
  return $content;
share|improve this answer
Thanks, Why did you not use gettype() for determining type of a variable easily? –  PHPst Mar 13 '13 at 8:51
There is a warning on the german gettype php documentation page: php.net/manual/de/function.gettype.php - Translated "for performance reasons" ;-). –  chris Mar 13 '13 at 9:33
get_type() is a user defined function - gettype() is a php function. The underscore is important. –  chris Apr 18 '13 at 9:42
add comment

if you just want to see the var_dump out put in the firebug (client side) without doing any things in Javascript I would recommande using cookies following is an illustration how you can perform it that way:

$str =  "Abu Romaïssae";


echo "<pre>";
echo $str."\n";

function sendVarDumpToFront( $mixed ){
    $content = ob_get_contents();

than you can have it in firebug this way:

reading cookie content from Firebug


since this way uses cookies you will have to put the var_dump content before outputing any content otherwise this will not work

share|improve this answer
Thanks for your new idea. –  PHPst Mar 10 '13 at 4:57
add comment

From: http://sixrevisions.com/web-development/how-to-debug-php-using-firefox-with-firephp/ Fb::log($array, "dumping an array") That will get you the type and data. You'll have to do extra logging manually for length/count.

share|improve this answer
add comment

The following will take anything from var_dump() and encode it in JSON before attempting to send it to console.log(). This prevents and special characters from messing up the output.

$myArray = array('Red','Green','Blue','Orange','Yellow','Purple');

$var_dump = ob_get_contents();

var var_dump = <?php echo json_encode($var_dump); ?>;
share|improve this answer
add comment

If you have an Ajax (XHR) call that is generating output with a var_dump() then you can inspect the request in FireBug either under 'Console' or 'Net'. Click the plus sign to expand it and look at the 'Response' tab.

Otherwise if you are putting var_dump() into the main page you are viewing it should just appear in the page as viewed although the formatting might be messed up. Try

echo '<PRE>' 

before the var_dump() or alternatively view the page source rather than the direct output.

share|improve this answer
add comment

I think one easy way to achieve this goal is to do a simple

console.log(<?php var_export($var, true) ?>);

share|improve this answer
add comment

You are over complicating what is a simple issue. Firebug (and any other console/dom log viewer is for viewing client side output. PHP being server side and doesn't make much sense pushing to the console log.

With that said, if you REALLY wanted to pipe a server-side output to the console log you should convert that output into json and pass it onto the console log. If you simply want to output variable values on a life site without people knowing that you are working on it (and you shouldn't be working on a live version anyway but that's beside the point) why not pipe the output to a file and read that output however you like, you could even use ajax to pass the dump off to the log via jquery.

The point I am trying to make is...you are over-complicating what you are trying to do.

share|improve this answer
add comment

only from JavaScript a jquery From arrays in firebug and chrome is:

 console.dir('[object arrays]'); 

open the console and activate to F12... and write this code in console is the var_dump to php from jquery. array to json

var test = {"names":["john doe","JANE doe"],"ids":["123",null]}; console.dir(test);

if you need directly console fron PHP need a plugin

share|improve this answer
Did you read the question correctly? It's about PHP!!! –  PHPst Feb 21 '13 at 9:21
add comment

If you want to use exactly "var_dump" function name with different behavior than provides var_dump you may use runkit runkit_function_rename and write your own var_dump function using renamed function.

But, i suppose, it will be hard to install. But i can be wrong.

share|improve this answer
Doesn't have anything to do with how to get the value into FireBug; altering the behavior of core functions is a bad idea. –  Brad Koch Mar 9 '13 at 18:57
add comment

Your Answer


