如何用php回显到调试控制台

[英]How to echo to debug console with php


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或其他命令,但我需要输出显示在输出控制台中,而不是浏览器窗口中。

5 个解决方案

#1


25  

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

#2


4  

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_encode right in the echo statement. You will avoid your script to break if there are quotes in your $msg variable.

编辑:在echo语句中添加json_encode。如果您的$msg变量中有引号,您将避免脚本中断。

#3


1  

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

#4


1  

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中有很多更好的方法来打印变量的值。其中之一是使用buildin 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.com It 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.

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

#5


0  

You can also try this way:

你也可以这样尝试:

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2015/05/20/1162fd25445155a66c379f8a951755cc.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告