简体   繁体   English

PHP需要返回echo吗?

[英]PHP require return echo?

Is it possible to require a php file and get all the things that's echo'ed to be returned and stored into a variable? 是否有可能需要一个php文件并将所有回显的内容返回并存储到变量中?

Example: 例:

//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.

Use output buffering: 使用输出缓冲:

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

Output buffering can do what you need. 输出缓冲可以满足您的需求。

ob_start();
require("file1.php");
$file1 = ob_get_contents();
ob_clean();
ob_start();
include('file1.php');
$contents = ob_get_clean();

The output from file1.php is now stored in the variable $contents. file1.php的输出现在存储在变量$ contents中。

Output buffering : 输出缓冲

<?php

ob_start();

require 'file1.php';

$var_buffer = ob_get_contents();

ob_end_clean();

echo $var_buffer;

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM