php - 在 ob_start() 中使用 include()

标签 php

这里需要一些 PHP 帮助,包含的内容显示为“1”,这意味着它是真实的,但需要它的内容才能显示,我不确定为什么不是。这是一个函数的简化版本:

public function content {
    $website->content = 'Some content here. ';
    ob_start();
    include('myfile.php');
    $file_content = ob_end_clean();
    $website->content .= $file_content;
    $website->content .= ' Some more content here.';
     echo $website->content;
}

这个输出:

Some content here 
1 
Some more content here

当我需要它输出时:

Some content here
'Included file content here'
Some more content here

知道如何解决这个问题吗?

最佳答案

尝试使用 ob_get_clean() 而不是 ob_end_clean()

两者都清除当前缓冲区,但 ob_end_clean() 返回一个 bool 值,而 ob_get_clean() 返回当前缓冲区的内容。

关于php - 在 ob_start() 中使用 include(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8510532/

相关文章:

php - 使用 docker-compose 保持容器事件并链接

php - 用于插入的选项值

php - 是否可以在 yii2 PHP 框架中获取 secret 访问的 Controller 或操作 ID

php - 如何在字符串中从末尾的固定位置插入一个字符?

php - 如何在我的数据库中存储 $_SESSION 值

php - 有人让 aol oauth api 工作吗?

php - 警告 php_network_getaddress

php - 获取 3 天前发布的帖子的正确方法?

php mysql 仅保留单词和标点符号,没有制表符、换行符等

php - Windows 上的 Behat - 截图