如何在 PHP 中写入控制台?
是否可以将字符串或日志写入控制台?
我的意思
就像在 JSP 中一样,如果我们打印类似 system.out.println("some")
的内容,它将出现在控制台上,而不是页面上。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否可以将字符串或日志写入控制台?
就像在 JSP 中一样,如果我们打印类似 system.out.println("some")
的内容,它将出现在控制台上,而不是页面上。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(30)
或者您使用PHP 调试到控制台中的技巧。
首先,您需要一个 PHP 辅助函数
然后您可以像这样使用它:
这将创建如下输出:
Or you use the trick from PHP Debug to console.
First you need a little PHP helper function
Then you can use it like this:
This will create an output like this:
Firefox
在 Firefox 上,您可以使用名为 FirePHP 的扩展,它可以记录和转储信息从 PHP 应用程序发送到控制台。这是很棒的 Web 开发扩展 Firebug 的插件。
Chrome
但是,如果您使用 Chrome,则有一个名为 Chrome 记录器 或 webbug ( webug 的日志顺序有问题)。
最近,Clockwork 正在积极开发中,它通过添加新面板来提供有用的调试来扩展开发人员工具和分析信息。它为 Laravel 4 和 Slim 2 和支持可以通过其可扩展 API 添加。
使用 Xdebug
调试 PHP 的更好方法是通过 Xdebug。大多数浏览器提供帮助程序扩展来帮助您传递所需的 cookie/查询字符串来初始化调试过程。
Firefox
On Firefox you can use an extension called FirePHP which enables the logging and dumping of information from your PHP applications to the console. This is an addon to the awesome web development extension Firebug.
Chrome
However if you are using Chrome there is a PHP debugging tool called Chrome Logger or webug (webug has problems with the order of logs).
More recently Clockwork is in active development which extends the Developer Tools by adding a new panel to provide useful debugging and profiling information. It provides out of the box support for Laravel 4 and Slim 2 and support can be added via its extensible API.
Using Xdebug
A better way to debug your PHP would be via Xdebug. Most browsers provide helper extensions to help you pass the required cookie/query string to initialize the debugging process.
如果您正在寻找一种简单的方法,请 echo as JSON:
If you're looking for a simple approach, echo as JSON:
尝试以下操作。它正在工作:
Try the following. It is working:
默认情况下,所有输出都会发送到
stdout
,即 HTTP 响应或控制台,具体取决于您的脚本是由 Apache 运行还是在命令行上手动运行。但是您可以使用error_log
进行日志记录和各种 I/O 流可以通过fwrite
。By default, all output goes to
stdout
, which is the HTTP response or the console, depending on whether your script is run by Apache or manually on the command line. But you can useerror_log
for logging and various I/O streams can be written to withfwrite
.作为流行答案中链接网页的作者,我想添加这个简单辅助函数的最新版本。它更加坚固。
我使用 json_encode() 来检查变量类型是否不必要,并添加缓冲区来解决框架问题。没有可靠的回报或过度使用
header()
。使用
结果的屏幕截图
另外,一个简单的示例作为图像可以更容易地理解它:
As the author of the linked webpage in the popular answer, I would like to add my last version of this simple helper function. It is much more solid.
I use
json_encode()
to check if the variable type is unnecessary and add a buffer to solve problems with frameworks. There are no solid returns or excessive usage ofheader()
.Usage
Screenshot of the result
Also, a simple example as an image to understand it much easier:
PHP 和 JavaScript 交互。
PHP and JavaScript interaction.
创建一个
使用 the
,以便不显示 div,但该
函数是在 javascript 中创建的。这样您就可以在控制台中看到该消息。
Creates a
with the
so that the div is not displayed, but the
function is created in javascript. So you get the message in the console.
我觉得可以用——
I think it can be used --
一些很好的答案可以增加更多的深度;但我需要一些更简单、更像 JavaScript
console.log()
命令的东西。我在 Ajax 应用程序中的许多“收集数据并转换为 XML”中使用 PHP。 JavaScript
console.log
在这种情况下不起作用;它破坏了 XML 输出。Xdebug等也有类似的问题。
我在 Windows 中的解决方案:
.txt
文件.ini
文件中设置 PHPerror_log
变量写入该文件error_log('myTest');
PHP 命令发送消息这个解决方案很简单,大部分时间都能满足我的需求。标准 PHP,预览窗格会在每次 PHP 写入时自动更新。
Some great answers that add more depth; but I needed something simpler and more like the JavaScript
console.log()
command.I use PHP in a lot of "gathering data and turn into XML" in Ajax applications. The JavaScript
console.log
doesn't work in that case; it breaks the XML output.Xdebug, etc. had similar issues.
My solution in Windows:
.txt
file that is somewhat easily to get to and writableerror_log
variable in the.ini
file to write to that fileerror_log('myTest');
PHP command to send messagesThis solution is simple and meets my needs most of the time. Standard PHP, and the preview pane automatically updates every time PHP writes to it.
我认为最好的解决方案是使用
错误日志(内容)
这是输出
编辑 2022:
所以我发现了更好的方法,那就是
file_put_contents( "php://stdout", content)
它在没有日志信息的情况下写入
I think best solution is to use
error_log(content)
This is output
Edit 2022:
So I’ve discovered way better way and thats
file_put_contents("php://stdout", content)
It writes without the logging info
我发现这很有帮助:
并像这样使用它:
控制台中的输出:
您可以通过使用 $debug 值限制不太重要的日志来关闭它们。
I find this helpful:
And use it like:
Which outputs in console:
And you can switch off less important logs by limiting them using the $debug value.
简短而简单,适用于数组、字符串或对象。
Short and easy, for arrays, strings or also objects.
对于 Chrome,有一个名为 Chrome Logger 的扩展,允许记录 PHP 消息。
Firefox DevTools 甚至集成了对 Chrome Logger 协议的支持。
要启用日志记录,您只需保存 'ChromePhp.php' 文件 在你的项目中。然后可以像这样使用:
示例取自 GitHub 页面。
输出可能如下所示:
For Chrome there is an extension called Chrome Logger allowing to log PHP messages.
The Firefox DevTools even have integrated support for the Chrome Logger protocol.
To enable the logging, you just need to save the 'ChromePhp.php' file in your project. Then it can be used like this:
Example taken from the GitHub page.
The output may then look like this:
如果您想写入 PHP 日志文件,而不是 JavaScript 控制台,您可以使用以下命令:
error_log("This islogging only to the PHP log")
参考:error_log
If you want write to the PHP log file, and not the JavaScript console you can use this:
error_log("This is logged only to the PHP log")
Reference: error_log
还有一个很棒的 Google Chrome 扩展程序,PHP 控制台,其中 < a href="https://github.com/barbushin/php-console/#php-console-server-library" rel="nofollow noreferrer">PHP 库,它允许您:
error file:line
。There is also a great Google Chrome extension, PHP Console, with a PHP library that allows you to:
error file:line
in your text editor.这是我的解决方案,这个方案的好处是你可以传递任意数量的参数。
这样调用
现在您应该能够在控制台中看到输出,祝您编码愉快:)
Here is my solution, the good thing about this one is that you can pass as many params as you like.
Call it this way
Now you should be able to see output in your console, happy coding :)
干净、快速、简单,没有无用的代码:
Clean, fast and simple without useless code:
简短而简单地使用
printf
和json_encode
:Short and simply with
printf
andjson_encode
:这两个中的任何一个都在工作:
Any of these two are working:
我正在寻找一种在我正在开发的 WordPress 插件中调试代码的方法,并发现了这篇文章。
我从其他回复中获取了最适合我的代码,并将它们组合成一个可用于调试 WordPress 的函数。该函数为:
用法如下:
如果该函数用于WordPress开发,则该函数应放置在子主题的
functions.php
文件中,然后可以在代码中的任何位置调用。I was looking for a way to debug code in a WordPress plugin that I was developing and came across this post.
I took the bits of code that are most applicable to me from other responses and combined these into a function that I can use for debugging WordPress. The function is:
Usage is as follows:
If this function is used with WordPress development, the function should be placed in the
functions.php
file of the child theme and can then be called anywhere in the code.对于 Ajax 调用或 XML / JSON 响应,如果您不想弄乱正文,则需要通过 HTTP 标头发送日志,然后使用 Web 扩展将它们添加到控制台。 FirePHP(不再可用)和 QuantumPHP(ChromePHP 的一个分支)在 Firefox 中就是这样做的。
如果您有耐心,x-debug 是一个更好的选择 - 您可以更深入地了解 PHP,并且能够暂停脚本,查看发生了什么,然后恢复脚本。
For Ajax calls or XML / JSON responses, where you don't want to mess with the body, you need to send logs via HTTP headers, then add them to the console with a web extension. This is how FirePHP (no longer available) and QuantumPHP (a fork of ChromePHP) do it in Firefox.
If you have the patience, x-debug is a better option - you get deeper insight into PHP, with the ability to pause your script, see what is going on, then resume the script.
我已经放弃了上述所有内容,转而使用 调试器和记录器。我对它赞不绝口!
只需单击右上角的选项卡之一,或单击“单击此处”即可展开/隐藏。
注意不同的“类别”。您可以单击任何阵列来展开/折叠它。
从网页上
I have abandoned all of the above in favour of Debugger & Logger. I cannot praise it enough!
Just click on one of the tabs at top right, or on the "click here" to expand/hide.
Notice the different "categories". You can click any array to expand/collapse it.
From the web page
自 2017 年起,Firebug 以及 FirePHP 已被禁用。
我对 ChromePHP 工具进行了一些小修改,以允许从 FirePHP 无缝迁移到 Firebug,以便通过控制台进行调试。
本文以清晰简单的步骤解释了
5 分钟内从 FirePHP 迁移到 ChromePHP(不破坏现有代码)
As of 2017, Firebug and hence FirePHP has been disabled.
I wrote some little modifications to the ChromePHP tool to allow seamless migration from FirePHP to Firebug for debugging via the console.
This article explains in clear easy steps
Migrate from FirePHP to ChromePHP in 5 minutes (without breaking existing code)
我可能会迟到,但我正在寻找一种日志记录功能的实现,该功能:
console.log()
一样,console.log()
不同。所以输出看起来像这样:
(下面的代码片段在 php
7.2.11
上进行了测试。我不确定它的 php 向后兼容性。这可能是一个问题对于 javascript 也是如此(旧浏览器的术语),因为它会在console.log()
参数之后创建一个尾随逗号 - 这在ES 2017
之前是不合法的。)I might be late for a party, but I was looking for an implementation of logging function which:
console.log()
,console.log()
.So the output looks like that:
(The snippet below is tested on php
7.2.11
. I'm not sure about its php backward compatibility. It can be an issue for javascript as well (in a term of old browsers), because it creates a trailing comma afterconsole.log()
arguments – which is not legal untilES 2017
.)虽然这是一个老问题,但我一直在寻找这个。这是我在这里回答的一些解决方案以及其他地方找到的一些其他想法的汇编,以获得一刀切的解决方案。
代码:
测试和测试用法:
用法很简单。包括手动发布到控制台的第一个功能。使用第二个函数来转移 php 异常处理。以下测试应该给出一个想法。
解释:
函数
console($data, $is_error, $file, $fn)
将字符串或数组作为第一个参数,并使用 js 插入将其发布到控制台.第二个参数是一个标志,用于区分正常日志和错误日志。对于错误,我们添加了事件侦听器,以便在抛出任何错误时通过警报通知我们,并在控制台中突出显示。该标志默认为 false。
第三个和第四个参数是文件号和行号的显式声明,这是可选的。如果不存在,它们默认使用预定义的 php 函数
debug_backtrace()
为我们获取它们。下一个函数
exceptions_to_console($svr, $str, $file, $ln)
有四个参数,按php默认异常处理程序调用的顺序排列。在这里,第一个参数是严重性,我们使用函数severity_tag($code)
与预定义常量进一步交叉检查,以提供有关错误的更多信息。注意:
以上代码使用了旧版浏览器中不可用的 JS 函数和方法。为了与旧版本兼容,需要更换。
以上代码用于测试环境,只有您可以访问该网站。不要在实时(生产)网站中使用它。
建议:
第一个函数
console()
抛出了一些通知,因此我将它们包装在另一个函数中,并使用错误控制运算符“@”调用它。如果您不介意这些通知,则可以避免这种情况。最后但并非最不重要的一点是,编码时弹出的警报可能会很烦人。为此,我使用此蜂鸣声(在解决方案中找到:https://stackoverflow.com/a/23395136/6060602 )而不是弹出警报。它非常酷,可能性无穷无尽,您可以播放自己喜欢的音乐,减轻编码压力。
Though this is an old question, I've been looking for this. Here's my compilation of some solutions answered here and some other ideas found elsewhere to get a one-size-fits-all solution.
CODE :
TESTS & USAGE :
Usage is simple. Include first function for posting to console manually. Use second function for diverting php exception handling. Following test should give an idea.
EXPLANATION :
The function
console($data, $is_error, $file, $fn)
takes string or array as first argument and posts it on console using js inserts.Second argument is a flag to differentiate normal logs against errors. For errors, we're adding event listeners to inform us through alerts if any errors were thrown, also highlighting in console. This flag is defaulted to false.
Third and fourth arguments are explicit declarations of file and line numbers, which is optional. If absent, they're defaulted to using the predefined php function
debug_backtrace()
to fetch them for us.Next function
exceptions_to_console($svr, $str, $file, $ln)
has four arguments in the order called by php default exception handler. Here, the first argument is severity, which we further crosscheck with predefined constants using functionseverity_tag($code)
to provide more info on error.NOTICE :
Above code uses JS functions and methods that are not available in older browsers. For compatibility with older versions, it needs replacements.
Above code is for testing environments, where you alone have access to the site. Do not use this in live (production) websites.
SUGGESTIONS :
First function
console()
threw some notices, so I've wrapped them within another function and called it using error control operator '@'. This can be avoided if you didn't mind the notices.Last but not least, alerts popping up can be annoying while coding. For this I'm using this beep (found in solution : https://stackoverflow.com/a/23395136/6060602) instead of popup alerts. It's pretty cool and possibilities are endless, you can play your favorite tunes and make coding less stressful.
这是一个方便的功能。它使用起来非常简单,允许您传递任意数量的任何类型的参数,并将在浏览器控制台窗口中显示对象内容,就像您从 JavaScript 调用 console.log 一样 - 但从 PHP
注意,您可以也可以通过传递“TAG-YourTag”来使用标签,并且它将被应用,直到读取另一个标签,例如“TAG-YourNextTag”
注意:func_num_args()和func_num_args() 是用于读取动态数量的输入参数的 PHP 函数,并允许该函数从一个函数调用中发出无限多个 console.log 请求。
Here's a handy function. It is super simple to use, allows you to pass as many arguments as you like, of any type, and will display the object contents in the browser console window as though you called console.log from JavaScript - but from PHP
Note, you can use tags as well by passing 'TAG-YourTag', and it will be applied until another tag is read, for example, 'TAG-YourNextTag'
NOTE: func_num_args() and func_num_args() are PHP functions for reading a dynamic number of input arguments, and allow this function to have infinitely many console.log requests from one function call.
使用:
Use:
在开始代码中...
它可以工作
in start code...
it work