可以捕获PHP回声输出吗?

时间:2021-06-08 00:05:33

So I have a function such as:

所以我有一个如下功能:

public static function UnorderedList($items, $field, $view = false){
    if(count($items) > 0){
        echo '<ul>';
        foreach($items as $item){
            echo '<li>';
            if($view){
                echo '<a href="'.$view.'id='.$item->sys_id.'" title="View Item">'.$item->$field.'</a>';
            }else{
                echo $item->$field;
            }   
            echo '</li>';
        }
        echo '</ul>'; 
    }else{
        echo '<p>No Items...</p>';
    }
}

This function loops over some items and renders a unordered list. What I am wondering is if its possible to capture the echo output if I wish.

此函数循环一些项目并呈现无序列表。我想知道的是,如果我愿意,它是否可以捕获回声输出。

I make a call to use this function by doing something like:

我通过执行以下操作来调用此函数:

Render::UnorderedList(Class::getItems(), Class::getFields(), true); 

And this will dump a unordered list onto my page. I Know I can just change echo to a variable and return the variable but I was just curious if its possible to capture echo output without modifying that function, merely modifying the call to the function in some way?

这会将无序列表转储到我的页面上。我知道我可以将echo更改为变量并返回变量,但我只是好奇是否有可能在不修改该函数的情况下捕获echo输出,只是以某种方式修改对函数的调用?

Thanks!

2 个解决方案

#1


43  

Yes, using output buffering.

是的,使用输出缓冲。

<?php
ob_start(); // Start output buffering

Render::UnorderedList(Class::getItems(), Class::getFields(), true); 

$list = ob_get_contents(); // Store buffer in variable

ob_end_clean(); // End buffering and clean up

echo $list; // will contain the contents
 ?>

#2


0  

Very Similar to previous answer but a little more concise for my purposes:

与之前的答案非常相似,但为我的目的更简洁:

<?php
ob_start(); // Start output buffering

Render::UnorderedList(Class::getItems(), Class::getFields(), true); 

$list = ob_get_clean(); // Store buffer AND cleans it

echo $list; // will contain the contents
?>

I also want to mention how useful this is for PHP unit testing so as not to clutter your test logs with the output of what you are testing unless the test fails. Here is another stackflow answer related to this because I found this answer first on my google search when I was looking at how to test items with echo output : How to use output buffering inside PHPUnit test?

我还想提一下这对PHP单元测试有多大用处,以免你的测试日志与测试的输出混乱,除非测试失败。这是与此相关的另一个stackflow答案,因为当我在查看如何使用echo输出测试项目时,我首先在谷歌搜索中找到了这个答案:如何在PHPUnit测试中使用输出缓冲?

#1


43  

Yes, using output buffering.

是的,使用输出缓冲。

<?php
ob_start(); // Start output buffering

Render::UnorderedList(Class::getItems(), Class::getFields(), true); 

$list = ob_get_contents(); // Store buffer in variable

ob_end_clean(); // End buffering and clean up

echo $list; // will contain the contents
 ?>

#2


0  

Very Similar to previous answer but a little more concise for my purposes:

与之前的答案非常相似,但为我的目的更简洁:

<?php
ob_start(); // Start output buffering

Render::UnorderedList(Class::getItems(), Class::getFields(), true); 

$list = ob_get_clean(); // Store buffer AND cleans it

echo $list; // will contain the contents
?>

I also want to mention how useful this is for PHP unit testing so as not to clutter your test logs with the output of what you are testing unless the test fails. Here is another stackflow answer related to this because I found this answer first on my google search when I was looking at how to test items with echo output : How to use output buffering inside PHPUnit test?

我还想提一下这对PHP单元测试有多大用处,以免你的测试日志与测试的输出混乱,除非测试失败。这是与此相关的另一个stackflow答案,因为当我在查看如何使用echo输出测试项目时,我首先在谷歌搜索中找到了这个答案:如何在PHPUnit测试中使用输出缓冲?