Pass value of a include to a variable

2019-05-20 02:52发布

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.

2条回答
Anthone
2楼-- · 2019-05-20 03:46

use

ob_start();
include('example.html');
$output = ob_get_clean();// this will buffer your output
查看更多
看我几分像从前
3楼-- · 2019-05-20 03:47

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);
查看更多
登录 后发表回答