Pass value of a include to a variable

谁都会走 提交于 2020-01-02 11:31:32

问题


Is it possible to pass the result of an include to a variable?

Let's consider this simple file 'example.html':

<p>some HTML</p>

In a PHP file, how can we pass the content of this file to a variable? I tried:

$my_variable = include('example.html');

It includes the file, but the value of $my_variable is 1.


回答1:


If you just want to get the content of file you can use this :

$data = file_get_contents("example.html");
echo htmlentities($data);

or this :

ob_start();
readfile("example.html");
$data = ob_get_clean();
echo htmlentities($data);



回答2:


use

ob_start();
include('example.html');
$output = ob_get_clean();// this will buffer your output


来源:https://stackoverflow.com/questions/29543504/pass-value-of-a-include-to-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!