是否可以写入字符串或日志到控制台?

我的意思是

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


当前回答

我认为最好的解决办法是使用 error_log(内容) 这是输出

2022年编辑:

所以我发现了更好的方法,那就是file_put_contents("php://stdout",内容)

它在没有日志信息的情况下进行写入

其他回答

一些很好的答案,增加了更多的深度;但我需要一些更简单、更像JavaScript console.log()命令的东西。

我在Ajax应用程序的许多“收集数据并将其转换为XML”中使用了PHP。JavaScript console.log在这种情况下不起作用;它中断XML输出。

Xdebug等也有类似的问题。

我在Windows上的解决方案:

设置一个易于获取和写入的.txt文件 在.ini文件中设置PHP error_log变量以写入该文件 在Windows文件资源管理器中打开该文件,并为其打开预览窗格 使用error_log('myTest');PHP命令发送消息

这个解决方案很简单,大部分时间都能满足我的需求。标准PHP,并且每次PHP写入预览窗格时,预览窗格都会自动更新。

这里有一个方便的函数。它使用起来超级简单,允许您传递任意类型的任意数量的参数,并将在浏览器控制台窗口中显示对象内容,就像从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请求。

作为热门答案中链接网页的作者,我想添加我最后一个版本的这个简单的助手功能。它更坚固。

我使用json_encode()来检查变量类型是否不必要,并添加一个缓冲区来解决框架的问题。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 variable, object; here an array.
$data = [ 'foo' => 'bar' ];
debug_to_console($data);`

结果截图

另外,一个简单的例子作为一个图像,更容易理解它:

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

创建一个

<div>

display="none"

这样就不会显示div,而是

console.log()

函数是用javascript创建的。因此,您在控制台中获得消息。

对于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页面。

输出可能是这样的: