Sending “var_dump” to FireBug console

前端 未结 12 1229
忘掉有多难
忘掉有多难 2020-12-31 13:11

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

Is there any way to log its output to

相关标签:
12条回答
  • 2020-12-31 13:18

    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.

    0 讨论(0)
  • 2020-12-31 13:21

    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.

    0 讨论(0)
  • 2020-12-31 13:22
    <script>console.log( <?= json_encode( $var ) ?> )</script>
    

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

    0 讨论(0)
  • 2020-12-31 13:23

    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:

    <?php
    $str =  "Abu Romaïssae";
    
    sendVarDumpToFront($str);
    
    echo "<pre>";
    echo $str."\n";
    
    function sendVarDumpToFront( $mixed ){
        ob_start();
        var_dump($mixed);
        $content = ob_get_contents();
        ob_end_clean();
        setcookie("var_dump",$content);
    }
    

    than you can have it in firebug this way:

    reading cookie content from Firebug

    IMPORTANT

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

    0 讨论(0)
  • 2020-12-31 13:24

    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(
        1,
        'a',
        true
      ) ) );
    
    0 讨论(0)
  • 2020-12-31 13:26

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

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

    0 讨论(0)
提交回复
热议问题