在<?php?>之间的PHP eval()代码.来自数据库 [英] PHP eval() code in between <?php ?> from database
问题描述
我希望能够将PHP放入数据库并运行它.之所以必须这样做,是因为我将页面布局存储在数据库中,并且每个页面的布局彼此不同,但是在某些情况下,我想对某些页面使用动态内容.
I want to be able to put PHP into the database and run it. I have to do this because I store page layouts in the database and each our different for each other, however in some cases I want to use dynamic content for some of the pages.
假定$query_from_db
是从数据库返回的字符串. PHP仅应eval()在<?php
和?>
Assume $query_from_db
is the string returned from the database. PHP should only eval() the code in between <?php
and ?>
$query_from_db = '<div>
<?php
//php to run
function dosomething() {
//bleh
}
?>
</div>
';
php echo eval($query_from_db);
我该怎么做?我知道不建议这样做.
How can I do this? I'm aware this is not recommended.
推荐答案
我不是在讨论这种方法的意义或废话.从某种程度上说,这是一个有效的问题.
I'm not arguing about the sense or nonsense of this approach. To some extend, this is a valid question.
请参阅文档:
要混合HTML输出和PHP代码,可以使用封闭的PHP标记退出PHP模式.
To mix HTML output and PHP code you can use a closing PHP tag to leave PHP mode.
所以您必须这样做:
eval('?> ' . $query_from_db . ' <?php ');
还请注意,eval
直接输出到浏览器.它不返回值.看看 输出控制功能 缓冲.
Also note that eval
is outputting directly to the browser. It does not return a value. Have a look at Output Control Functions for buffering.
这篇关于在<?php?>之间的PHP eval()代码.来自数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!