我如何写入PHP的控制台?

是否可以写入string或login到控制台?

我的意思是

就像在jsp中一样,如果我们打印诸如system.out.println("some")东西,它将在控制台而不是在页面。

火狐

在Firefox上,您可以使用名为FirePHP的扩展名,它可以将您的PHP应用程序中的信息logging和转储到控制台。 这是一个壮观的Web开发扩展Firebug的插件。

但是,如果您使用的是Chrome,则有一个名为webug或Chrome Logger的PHPdebugging工具。

最近Clockwork正在积极开发中,通过添加一个新的面板来扩展开发工具,以提供有用的debugging和分析信息。 它为Laravel 4和Slim 2提供了开箱即用的支持,并且可以通过其可扩展API添加支持。

使用Xdebug

debugging你的PHP更好的方法是通过Xdebug 。 大多数浏览器提供帮助程序扩展来帮助您传递所需的cookie /查询string来初始化debugging过程。

  • Chrome – Xdebug助手
  • Firefox – 最简单的Xdebug
  • 歌剧 – Xdebug
  • Safari – Xdebug Toggler

或者你使用这个网站的PHPdebugging控制台

首先你需要一个小的PHP帮手function

 function debug_to_console( $data ) { $output = $data; if ( is_array( $output ) ) $output = implode( ',', $output); echo "<script>console.log( 'Debug Objects: " . $output . "' );</script>"; } 

那么你可以像这样使用它

 debug_to_console( "Test" ); 

这将创build一个像这样的输出:

 Debug Objects: Test 

如果你正在寻找一个简单的方法,回声作为JSON:

 <script> console.log(<?= json_encode($foo); ?>); </script> 

默认情况下,所有输出都转到stdout (这是HTTP响应或控制台),具体取决于脚本是由Apache运行还是在命令行上手动运行。 但是,您可以使用error_log进行日志logging,并且可以使用fwrite写入各种I / Ostream 。

试试这个工作:

 echo("<script>console.log('PHP: ".$data."');</script>"); 

一些伟大的答案,增加更多的深度; 但我需要一些更简单,更像JS console.log()命令。

我在AJAX应用程序中使用PHP进行了很多“收集数据并转换成xml”。 JS console.log在这种情况下不起作用; 它打破了xml输出。 (也许有人有这个解决scheme?)

Xdebug等有类似的问题。

我在Windows中的解决scheme:

  • 设置一个.txt文件,这个文件有点容易获取和写入
  • .ini文件中的PHP error_logvariables设置为写入该文件
  • 在Windows文件浏览器中打开该文件并打开预览窗格
  • 使用error_log('myTest'); PHP命令发送消息

这个解决scheme很简单,最符合我的需求,标准的PHP,预览窗格每次PHP写入时自动更新。

我觉得这有帮助:

 function console($data, $priority, $debug) { if ($priority <= $debug) { if (is_array($data)) $output = '<script>console.log("' . str_repeat(" ", $priority-1) . implode( ",", $data) . '");</script>'; else $output = '<script>console.log("' . str_repeat(" ", $priority-1) . $data . '");</script>'; echo $output; } } 

并使用它:

 <?php $debug = 5; // All lower and equal priority logs will be displayed console('Important' ,1 , $debug); console('Less Important' ,2 , $debug); console('Even Less Important' ,5 , $debug); console('Again Important' ,1 , $debug); ?> 

控制台中的哪些输出:

 Important Less Important Even Less Important Again Important 

而且你可以通过使用$ debug值来限制不重要的日志

作为链接网页int的作者以上stream行的答案 ,我想添加我最后一个版本的这个简单的帮手function,更坚实。

我使用json_encode()来检查不需要的vartypes,并添加一个缓冲区来解决框架问题,但是没有可靠的返回或过多的使用header()

 /** * Simple helper to debug to the console * * @param $data object, array, string $data * @param $context string Optional a description. * * @return string */ function debug_to_console( $data, $context = 'Debug in Console' ) { // Buffering to solve problems frameworks, like header() in this and not a solid return. ob_start(); $output = 'console.info( \'' . $context . ':\' );'; $output .= 'console.log(' . json_encode( $data ) . ');'; $output = sprintf( '<script>%s</script>', $output ); echo $output; } 

用法

 // $data is the example var, object; here an array. $data = [ 'foo' => 'bar' ]; debug_to_console( $data );` 

结果的屏幕截图

还有一个简单的例子,就是要更容易理解图片。

在这里输入图像说明

 function phpconsole($label='var',$x){ ?> <script type="text/javascript"> console.log('<?php echo ($label)?>'); console.log('<?php echo json_encode($x)?>'); </script> <?php } 

对于数组,string或对象来说简单易行。

 function console_log( $data ) { $output = "<script>console.log( 'PHP debugger: "; $output .= json_encode(print_r($data, true)); $output .= "' );</script>"; echo $output; } 

我认为可以使用 –

 function jsLogs($data) { $html = ""; if(is_array($data) || is_object($data)) { $html = "<script>console.log('PHP: ".json_encode($data)."');</script>"; } else { $html = "<script>console.log('PHP: ".$data."');</script>"; } echo($html); # exit(); } jsLogs(array("test1", "test2")); # PHP: ["test1","test2"] jsLogs(array("test1"=>array("subtest1", "subtest2"))); #PHP: {"test1":["subtest1","subtest2"]} jsLogs("testing string"); #PHP: testing string 

如果你想写入PHP日志文件,而不是JavaScript控制台,你可以使用这个:

error_log ( "This is logged only to the PHP log" )

参考: http : //php.net/manual/en/function.error-log.php

对于Chrome,可以使用名为Chrome Logger的扩展程序来loggingPHP消息。

Firefox DevTools甚至已经集成了对Chrome Logger协议的支持 。

要启用日志logging,您只需要将“ChromePhp.php”文件保存在您的项目中。 那么它可以像这样使用:

 include 'ChromePhp.php'; ChromePhp::log('Hello console!'); ChromePhp::log($_SERVER); ChromePhp::warn('something went wrong!'); 

从GitHub页面取得的例子。

输出结果可能如下所示:

服务器日志在Firefox DevTools

还有很棒的谷歌浏览器扩展PHP控制台与PHP库 ,允许:

  • 在Chrome浏览器JavaScript控制台和通知popup窗口中查看错误和例外。
  • 转储任何types的variables。
  • 远程执行PHP代码。
  • 通过密码保护访问。
  • 按请求分组控制台日志。
  • 跳转到文本编辑器中的错误文件:行。
  • 将错误/debugging数据复制到剪贴板(对于testing人员)。

我放弃了所有上面的赞成http://phptoolcase.com/guides/ptc-debug-guide.html我不能称赞它够!;

只需点击右上angular的其中一个标签,或点击“点击此处”展开/隐藏。

注意不同的“类别”。 你可以点击任何数组展开/搭配它。

从网页上

“主要特点:

 Show globals vars ($GLOBALS, $_POST, $_GET, $_COOKIE ...) Show php version and loaded extensions Replace php built in error handler Log sql queries Monitor code and sql queries execution time Inspect variables for changes Function calls tracing Code coverage analysis to check which lines of script where executed Dump of all types of variable File inspector with code highlighter to view source code Send messages to js console(Chrome only), for ajax scripts 

在这里输入图像说明

这两个中的任何一个都在工作:

 <?php $five = 5; $six = 6; ?> <script> console.log(<?php echo $five + $six ?>); </script> <?php $five = 5; $six = 6; echo("<script>console.log($five + $six);</script>"); ?> 

伟大的职位谢谢,我正在寻找一种方法来debugging我开发的Wordpress插件中的代码,并遇到这个职位。

我从上面的回答中拿出了最适合我的代码,并将它们组合成一个可用于debuggingWordpress的函数。 function是:

 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 ); 

我希望别人觉得这个function有用。

如果这个函数和Wordpress开发一起使用的话,这个函数应该放在子主题的functions.php文件中,然后可以在代码的任何地方调用。

截至2017年,萤火虫和firephp已被禁用。

我为chromephp工具写了一些小修改,允许从firephp到firebug的无缝迁移,通过控制台进行debugging。

这篇文章解释清楚简单的步骤

https://medium.com/@kudehinbuoluwaponle/migrate-from-firephp-to-chromephp-in-5-minutes-without-breaking-existing-code-e4afd1b28c5c

 $variable = "Variable"; echo "<script>console.log('$variable');</script>"; 

PHP和Javascript交互。

  echo "<div display='none'><script type='text/javascript'>console.log('console log message')</script></div>"; 

创build一个

  <div> 

  display="none" 

所以div不显示,但是

  console.log() 

函数是在javascript中创build的。 所以你在控制台中得到消息。

 function console_log( $data ) { $bt = debug_backtrace(); $caller = array_shift($bt); if ( is_array( $data ) ) error_log( end(split('/',$caller['file'])) . ':' . $caller['line'] . ' => ' . implode( ',', $data) ); else error_log( end(split('/',$caller['file'])) . ':' . $caller['line'] . ' => ' . $data ); }