具有评估代码执行功能的heredoc
问题描述:
我尝试了几种方法来使它正常工作,但是没有运气!
I've tryed a couple of methods to try and get this working but with no luck!
我有一个这样的页面(示例):
I've got a page like this (Example):
<?php
$jj = <<<END
?>
<h1>blah blah</h1>
<p> blah blah blah blah blah blah blah <?php include("file.php"); ?> blah blah</p>
<?php
END;
eval('?>'.$jj.'<?php ');
?>
这导致什么都没输出,想不出解决办法!
this causes no output what so ever, can not think of a solution!
答
这将不起作用,因为eval仅需要PHP代码(即,不被<?php?>标记包围),因此对eval()的调用将可能会因解析错误而失败.
This will not work because eval only expects PHP code (i.e. not surrounded by <?php ?> tags), so the call to eval() will probably fail with a parse error.
我建议改用输出缓冲,例如:
I would suggest using output buffering instead, for example:
<?php
//start output buffering, anything outputted should be stored in a buffer rather than being sent to the browser
ob_start();
?>
<h1>blah blah</h1>
<p> blah blah blah blah blah blah blah <?php include("file.php"); ?> blah blah</p>
<?php
//get contents of buffer and stop buffering
$content = ob_get_clean();
echo $content;
?>