是否可以写入字符串或日志到控制台?
我的意思是
就像在JSP中一样,如果我们打印system.out.println("some")这样的东西,它将出现在控制台,而不是页面上。
是否可以写入字符串或日志到控制台?
我的意思是
就像在JSP中一样,如果我们打印system.out.println("some")这样的东西,它将出现在控制台,而不是页面上。
当前回答
$variable = "Variable";
echo "<script>console.log('$variable');</script>";
PHP和JavaScript交互。
其他回答
如果你想写入PHP日志文件,而不是JavaScript控制台,你可以使用这个:
error_log(“这只记录到PHP日志”)
参考:error_log
Use:
function console_log($data) {
$bt = debug_backtrace();
$caller = array_shift($bt);
if (is_array($data))
$dataPart = implode(',', $data);
else
$dataPart = $data;
$toSplit = $caller['file'])) . ':' .
$caller['line'] . ' => ' . $dataPart
error_log(end(split('/', $toSplit));
}
我已经放弃了上述所有工具,转而使用调试器和记录器。我怎么称赞都不为过!
只需点击右上角的一个标签,或者点击“点击这里”来展开/隐藏。
注意不同的“类别”。您可以单击任意数组展开/折叠它。
从网页
主要特点: 显示全局变量($ globals, $_POST, $_GET, $_COOKIE等) 显示PHP版本和加载的扩展 替换PHP内置的错误处理程序 记录SQL查询 监视代码和SQL查询的执行时间 检查变量的变化 函数调用跟踪 代码覆盖分析,检查执行了哪些脚本行 转储所有类型的变量 文件检查器与代码高亮显示查看源代码 发送消息到JavaScript控制台(仅限Chrome),用于Ajax脚本
简单的printf和json_encode:
function console_log($data) {
printf('<script>console.log(%s);</script>', json_encode($data));
}
我正在寻找一种方法来调试我正在开发的WordPress插件中的代码,然后看到了这篇文章。
我从其他回复中选取了最适合我的代码,并将它们组合成一个函数,我可以用它来调试WordPress。函数为:
function debug_log($object=null, $label=null, $priority=1) {
$priority = $priority<1? 1: $priority;
$message = json_encode($object, JSON_PRETTY_PRINT);
$label = "Debug" . ($label ? " ($label): " : ': ');
echo "<script>console.log('" . str_repeat("-", $priority-1) . $label . "', " . $message . ");</script>";
}
用法如下:
$txt = 'This is a test string';
$sample_array = array('cat', 'dog', 'pig', 'ant', 'fly');
debug_log($txt, '', 7);
debug_log($sample_array);
如果这个函数用于WordPress开发,那么这个函数应该放在子主题的functions.php文件中,然后可以在代码的任何地方调用。