问题
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