是否可以写入字符串或日志到控制台?
我的意思是
就像在JSP中一样,如果我们打印system.out.println("some")这样的东西,它将出现在控制台,而不是页面上。
是否可以写入字符串或日志到控制台?
我的意思是
就像在JSP中一样,如果我们打印system.out.println("some")这样的东西,它将出现在控制台,而不是页面上。
当前回答
这里有一个方便的函数。它使用起来超级简单,允许您传递任意类型的任意数量的参数,并将在浏览器控制台窗口中显示对象内容,就像从JavaScript调用console.log一样——但是从PHP调用的
注意,你也可以通过传递' tag - yourtag '来使用标签,它将被应用,直到读取另一个标签,例如' tag - yournexttag '
/*
* Brief: Print to console.log() from PHP
*
* Description: Print as many strings,arrays, objects, and
* other data types to console.log from PHP.
*
* To use, just call consoleLog($data1, $data2, ... $dataN)
* and each dataI will be sent to console.log - note
* that you can pass as many data as you want an
* this will still work.
*
* This is very powerful as it shows the entire
* contents of objects and arrays that can be
* read inside of the browser console log.
*
* A tag can be set by passing a string that has the
* prefix TAG- as one of the arguments. Everytime a
* string with the TAG- prefix is detected, the tag
* is updated. This allows you to pass a tag that is
* applied to all data until it reaches another tag,
* which can then be applied to all data after it.
*
* Example:
*
* consoleLog('TAG-FirstTag', $data, $data2, 'TAG-SecTag, $data3);
*
* Result:
* FirstTag '...data...'
* FirstTag '...data2...'
* SecTag '...data3...'
*/
function consoleLog(){
if(func_num_args() == 0){
return;
}
$tag = '';
for ($i = 0; $i < func_num_args(); $i++) {
$arg = func_get_arg($i);
if(!empty($arg)){
if(is_string($arg) && strtolower(substr($arg, 0, 4)) === 'tag-'){
$tag = substr($arg, 4);
}else{
$arg = json_encode($arg, JSON_HEX_TAG | JSON_HEX_AMP );
echo "<script>console.log('" . $tag . " " . $arg . "');</script>";
}
}
}
}
注意:func_num_args()和func_num_args()是PHP函数,用于读取动态数量的输入参数,并允许该函数从一个函数调用中有无限多个console.log请求。
其他回答
对于Ajax调用或XML / JSON响应,您不想干扰主体,您需要通过HTTP报头发送日志,然后使用web扩展将它们添加到控制台。这就是FirePHP(不再可用)和QuantumPHP (ChromePHP的一个分支)在Firefox中的做法。
如果您有耐心,x-debug是一个更好的选择—您可以更深入地了解PHP,能够暂停脚本,查看正在发生的事情,然后恢复脚本。
我已经放弃了上述所有工具,转而使用调试器和记录器。我怎么称赞都不为过!
只需点击右上角的一个标签,或者点击“点击这里”来展开/隐藏。
注意不同的“类别”。您可以单击任意数组展开/折叠它。
从网页
主要特点: 显示全局变量($ globals, $_POST, $_GET, $_COOKIE等) 显示PHP版本和加载的扩展 替换PHP内置的错误处理程序 记录SQL查询 监视代码和SQL查询的执行时间 检查变量的变化 函数调用跟踪 代码覆盖分析,检查执行了哪些脚本行 转储所有类型的变量 文件检查器与代码高亮显示查看源代码 发送消息到JavaScript控制台(仅限Chrome),用于Ajax脚本
在开始代码中…
error_reporting(-1);
ini_set('display_errors', 'On');
它的工作
火狐
在Firefox上,您可以使用一个名为FirePHP的扩展,它支持从PHP应用程序记录信息并将信息转储到控制台。这是一个令人敬畏的web开发扩展Firebug的插件。
http://www.studytrails.com/blog/using-firephp-in-firefox-to-debug-php/
铬
但是如果你使用的是Chrome浏览器,有一个PHP调试工具叫做Chrome Logger或webug (webug有日志顺序的问题)。
最近,Clockwork正在积极开发中,它通过添加一个新的面板来提供有用的调试和分析信息,扩展了开发人员工具。它为Laravel 4和Slim 2提供了开箱即用的支持,并可以通过其可扩展的API添加支持。
使用Xdebug
调试PHP的更好方法是通过Xdebug。大多数浏览器都提供了辅助扩展来帮助您传递所需的cookie/查询字符串来初始化调试过程。
Chrome - Xdebug助手 Firefox -最简单的Xdebug Opera - Xdebug Safari - Xdebug切换器
对于Chrome浏览器,有一个名为Chrome Logger的扩展,允许记录PHP消息。
Firefox DevTools甚至集成了对Chrome Logger协议的支持。
要启用日志记录,您只需要在项目中保存“ChromePhp.php”文件。那么它可以这样使用:
include 'ChromePhp.php';
ChromePhp::log('Hello console!');
ChromePhp::log($_SERVER);
ChromePhp::warn('something went wrong!');
示例取自GitHub页面。
输出可能是这样的: