Sending “var_dump” to FireBug console

2019-02-06 09:53发布

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

12条回答
时光不老,我们不散
2楼-- · 2019-02-06 10:07

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();
$channel->setResponse($response);

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

// record log messages
$logger->info('test');
$logger->info(var_export($_SESSION,true));
$logger->info(count(var_export($_SESSION,true)));
$logger->info(strlen(var_export('hello',true)));
$logger->info(get_type($_SESSION,true));  

// insert the wildfire headers into the HTTP response
$channel->flush();

// send the HTTP response headers
$response->sendHeaders();

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

<?php
function get_type($var) 
{
    if(is_object($var))
        return get_class($var);
    if(is_null($var))
        return 'null';
    if(is_string($var))
        return 'string';
    if(is_array($var))
        return 'array';
    if(is_int($var))
        return 'integer';
    if(is_bool($var))
        return 'boolean';
    if(is_float($var))
        return 'float';
    if(is_resource($var))
        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) {
  ob_start();
  var_dump($mixed);
  $content = ob_get_contents();
  ob_end_clean();
  return $content;
}
查看更多
狗以群分
3楼-- · 2019-02-06 10:07

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.

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

ob_start();
var_dump($myArray);
$var_dump = ob_get_contents();
ob_end_clean();
?>

<script>
var var_dump = <?php echo json_encode($var_dump); ?>;
console.log(var_dump);
</script>
查看更多
We Are One
4楼-- · 2019-02-06 10:12

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

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

查看更多
ゆ 、 Hurt°
5楼-- · 2019-02-06 10:16

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.

查看更多
够拽才男人
6楼-- · 2019-02-06 10:16

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

查看更多
【Aperson】
7楼-- · 2019-02-06 10:17

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.

查看更多
登录 后发表回答