如何使用 php 回显(或打印)到 js 控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30357773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:44:33  来源:igfitidea点击:

How to echo (or print) to the js console with php

php

提问by OB7

In javascript, I can print to the debug console using

在javascript中,我可以使用打印到调试控制台

console.log("Message here");

I'm now writing a php script, and would like to print to the debug console. Using the above code doesn't work in php. It seems I need to use either echo or some other command but I need the output to appear inside the output console, not the browser window.

我现在正在编写一个 php 脚本,并想打印到调试控制台。使用上面的代码在 php 中不起作用。似乎我需要使用 echo 或其他一些命令,但我需要输出出现在输出控制台中,而不是浏览器窗口中。

回答by arjabbar

<?php
   echo '<script>console.log("Your stuff here")</script>';
?>

回答by Lou

This will work with either an array, an object or a variable and also escapes the special characters that may break your JS :

这将适用于数组、对象或变量,并且还会转义可能会破坏您的 JS 的特殊字符:

function debugToConsole($msg) { 
        echo "<script>console.log(".json_encode($msg).")</script>";
}

Edit :Added json_encodeto the echostatement. This will prevent your script from breaking if there are quotes in your $msgvariable.

编辑:添加json_encodeecho语句中。如果您的$msg变量中有引号,这将防止您的脚本中断。

回答by Michael Khaikin

<?php  echo "<script>console.log({$yourVariable})</script>"; ?>

回答by Jaro Trapic

There are much better ways to print variable's value in PHP. One of them is to use buildin var_dump() function. If you want to use var_dump(), I would also suggest to install Xdebug (from https://xdebug.org) since it generates much more readable printouts.

有很多更好的方法可以在 PHP 中打印变量的值。其中之一是使用内置 var_dump() 函数。如果您想使用 var_dump(),我还建议安装 Xdebug(来自https://xdebug.org),因为它会生成更具可读性的打印输出。

The idea of printing values to browser console is somewhat bizarre, but if you really want to use it, there is very useful Google Chrome extension, PHP Console, which should satisfy all your needs. You can find it at consle.comIt works well also in Vivaldi and in Opera (though you will need "Download Chrome Extension" extension to install it). The extension is accompanied by PHP library you use in your code.

将值打印到浏览器控制台的想法有些奇怪,但是如果您真的想使用它,那么非常有用的 Google Chrome 扩展程序 PHP Console 应该可以满足您的所有需求。你可以在consle.com 上找到它它在 Vivaldi 和 Opera 中也能很好地工作(尽管你需要“下载 Chrome 扩展”扩展来安装它)。该扩展附带您在代码中使用的 PHP 库。

回答by Brad Kent

https://github.com/bkdotcom/PHPDebugConsole

https://github.com/bkdotcom/PHPDebugConsole

Support for all the javascript console methods:
assert, clear, count, error, group, groupCollapsed, groupEnd, info, log, table, trace, time, timeEnd, warn
plus a few more:
alert, groupSummary, groupUncollapse, timeGet

支持所有 javascript 控制台方法:
assert、clear、count、error、group、groupCollapsed、groupEnd、info、log、table、trace、time、timeEnd、warn
以及更多:
alert、groupSummary、groupUncollapse、timeGet

$debug = new \bdk\Debug(array(
    'collect' => true,
    'output' => true,
    'outputAs' => 'script',
));

$debug->log('hello world');
$debug->info('all of the javascript console methods are supported');
\bdk\Debug::_log('can use static methods');
$debug->trace();
$list = array(
    array('userId'=>1, 'name'=>'Bob', 'sex'=>'M', 'naughty'=>false),
    array('userId'=>10, 'naughty'=>true, 'name'=>'Sally', 'extracol' => 'yes', 'sex'=>'F'),
    array('userId'=>2, 'name'=>'Fred', 'sex'=>'M', 'naughty'=>false),
);
$debug->table('people', $list);

this will output the appropriate <script>tag upon script shutdown

这将<script>在脚本关闭时输出适当的标签

alternatively, you can output as html, chromeLogger, FirePHP, file, plaintext, websockets, etc

或者,您可以输出为 html、chromeLogger、FirePHP、文件、纯文本、websockets 等

upcomming release includes a psr-3 (logger) implementation

即将发布的版本包括 psr-3(记录器)实现

回答by Zohar Revivo

For something simple that work for arrays , strings , and objects I builed this function:

对于适用于数组、字符串和对象的简单东西,我构建了这个函数:

function console_testing($var){

    $var = json_encode($var,JSON_UNESCAPED_UNICODE);

    $output = <<<EOT
    <script>
        console.log($var); 
    </script>
EOT;

    echo $output;

}

回答by Marcelo Gumiero

You can also try this way:

你也可以试试这种方式:

<?php
   echo "<script>console.log('$variableName')</script>";
?>