在PHP中,有没有一种方法可以在不使用输出缓冲的情况下将PHP文件的输出捕获到变量中? [英] In PHP, is there a way to capture the output of a PHP file into a variable without using output buffering?
本文介绍了在PHP中,有没有一种方法可以在不使用输出缓冲的情况下将PHP文件的输出捕获到变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在PHP中,我想将一个文件读入一个变量,并在不使用输出缓冲的情况下同时处理该文件中的PHP.这可能吗?
In PHP, I want to read a file into a variable and process the PHP in the file at the same time without using output buffering. Is this possible?
基本上,我希望能够不使用ob_start()
来完成此操作:
Essentially I want to be able to accomplish this without using ob_start()
:
<?php
ob_start();
include 'myfile.php';
$xhtml = ob_get_clean();
?>
这在PHP中可行吗?
更新:我想在输出回调中做一些更复杂的事情(不允许输出缓冲).
Update: I want to do some more complex things within an output callback (where output buffering is not allowed).
推荐答案
PHP的一个鲜为人知的功能是能够将包含/必需的文件像函数调用一样对待,并带有返回值.
A little known feature of PHP is being able to treat an included/required file like a function call, with a return value.
例如:
// myinclude.php
$value = 'foo';
$otherValue = 'bar';
return $value . $otherValue;
// index.php
$output = include './myinclude.php';
echo $output;
// Will echo foobar
这篇关于在PHP中,有没有一种方法可以在不使用输出缓冲的情况下将PHP文件的输出捕获到变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文