是否可以写入字符串或日志到控制台?
我的意思是
就像在JSP中一样,如果我们打印system.out.println("some")这样的东西,它将出现在控制台,而不是页面上。
是否可以写入字符串或日志到控制台?
我的意思是
就像在JSP中一样,如果我们打印system.out.println("some")这样的东西,它将出现在控制台,而不是页面上。
当前回答
我正在寻找一种方法来调试我正在开发的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文件中,然后可以在代码的任何地方调用。
其他回答
我认为它可以被用来
function jsLogs($data, $isExit) {
$html = "";
$coll;
if (is_array($data) || is_object($data)) {
$coll = json_encode($data);
} else {
$coll = $data;
}
$html = "<script id='jsLogs'>console.log('PHP: ${coll}');</script>";
echo($html);
if ($isExit) exit();
}
# For String
jsLogs("Testing string"); #PHP: Testing string
# For Array
jsLogs(array("test1", "test2")); # PHP: ["test1","test2"]
# For Object
jsLogs(array("test1"=>array("subtest1", "subtest2"))); #PHP: {"test1":["subtest1","subtest2"]}
我正在寻找一种方法来调试我正在开发的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文件中,然后可以在代码的任何地方调用。
火狐
在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切换器
截至2017年,Firebug和FirePHP已被禁用。
我对ChromePHP工具进行了一些小修改,以允许从FirePHP无缝迁移到Firebug,以便通过控制台进行调试。
这篇文章用简单明了的步骤解释
5分钟内从FirePHP迁移到ChromePHP(不破坏现有代码)
echo
"<div display='none'>
<script type='text/javascript'>
console.log('console log message');
</script>
</div>";
创建一个
<div>
与
display="none"
这样就不会显示div,而是
console.log()
函数是用javascript创建的。因此,您在控制台中获得消息。