Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在ob_start()中使用include()_Php - Fatal编程技术网

Php 在ob_start()中使用include()

Php 在ob_start()中使用include(),php,Php,这里需要一点PHP帮助,包含的内容显示为'1',这意味着它是真实的,但需要它的内容出现,我不确定为什么它不是。以下是函数的简化版本: public function content { $website->content = 'Some content here. '; ob_start(); include('myfile.php'); $file_content = ob_end_clean(); $website->content .=

这里需要一点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 这里有一些内容 1. 这里有更多的内容 当我需要它输出时:

Some content here 'Included file content here' Some more content here 这里有一些内容 '此处包含文件内容' 这里有更多的内容
你知道我如何解决这个问题吗?

你应该使用,而不是。前者返回输出缓冲区的字符串并将其清空,而后者只执行清空并返回bool。

尝试使用
ob\u get\u clean()
而不是
ob\u end\u clean()


两者都清除当前缓冲区,但
ob\u end\u clean()
返回布尔值,而
ob\u get\u clean()
返回当前缓冲区的内容

不要使用
ob\u end\u clean
将现有代码修改为类似于以下内容的内容

ob_start();

include('myfile.php');

$file_content = ob_get_contents();

ob_end_clean ();
我之所以不投票支持ob_get_clean,是因为我曾经有过其他开发人员对实际情况感到困惑的时候,函数名并没有真正声明输出缓冲将在调用后结束

我认为这个函数很容易出现更多的bug,因此使用
ob\u start
/
ob\u end\u clean
更容易理解,更便于将来的代码维护

在一行额外的代码和更容易理解的代码之间进行权衡是非常值得的