我有一个像这样的外部php文件:
<!DOCTYPE>
<head>...
...
<body>
<p><?php echo $content;?></p>
..
在我的代码中:
$content = 'sample text';
$body = include("layout/mailtemplate.php");
你可以看到它有php和html代码(并将文件外的$content
传递给包含的文件)
有没有办法将此文件的内容存储到php变量? (这里$ body的内容是“1”!)
我也测试
$body = file_get_contents('layout/mailtemplate.php');
虽然有效,但我无法将$content
传递给档案。
(我知道我可以通过GET传递它),但我有很多变量。有更简单的方法吗?
答案 0 :(得分:0)
是的,你可以。您需要使用输出缓冲:
ob_start();
$content = 'sample text';
include("inc.php");
$body = ob_get_contents();
ob_end_clean();
var_dump($body); // string(11) "sample text"