Sending “var_dump” to FireBug console

前端 未结 12 1426
忘掉有多难
忘掉有多难 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:32

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

提交回复
热议问题