PHP需要返回echo吗?

时间:2011-04-16 23:50:24

标签: php

是否有可能需要一个php文件并将所有回显的内容返回并存储到变量中?

示例:

//file1.php
// let's say $somevar = "hello world"
<p><?php echo $somevar; ?></p>


//file2.php
$file1 = getEchoed("file1.php");
// I know getEchoed don't exist, but i'm unsure how to do it.

4 个答案:

答案 0 :(得分:11)

使用输出缓冲:

ob_start();
require('somefile.php');
$data = ob_get_clean();

答案 1 :(得分:1)

Output buffering可以做你需要的。

ob_start();
require("file1.php");
$file1 = ob_get_contents();
ob_clean();

答案 2 :(得分:1)

ob_start();
include('file1.php');
$contents = ob_get_clean();

file1.php的输出现在存储在变量$ contents。

答案 3 :(得分:0)

Output buffering

<?php

ob_start();

require 'file1.php';

$var_buffer = ob_get_contents();

ob_end_clean();

echo $var_buffer;

?>