使用 include/require_once 将内容分配给变量
是否可以这样做
$var = require_once('lol.php');
,以便 lol.php
所做的任何 HTML 输出都将进入 $var
内部?
我了解输出缓冲,但是是否有一些特殊的内置函数已经可以做到这一点?
Is it possible to do like
$var = require_once('lol.php');
so that any HTML output that lol.php
does will go inside $var
?
I know about output buffering, but is there some special built-in function that already does this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$var = require_once('lol.php');
只会将文件的返回值放入$var
中。如果您没有从中返回任何内容,则它只是null
。如果您想要输出,则需要使用输出缓冲:
$var = require_once('lol.php');
will only put the return value of the file into$var
. If you don't return anything from it, it'll just benull
.If you want the output you will need to use output buffering:
=include()
调用的赋值只会让您从该脚本获得可能的返回
值,而不是任何输出。为了实现这一点,您必须修改包含脚本以捕获输出:
The assignment from an
=include()
call will only get you a possiblereturn
value from that script, not any output.To make this possible you would have to modify the include script to capture the output: