简体   繁体   English

如何在运行时动态执行PHP命令?

[英]How to execute PHP commands dynamically at run time?

I wonder is there any way that let's to dynamically load a piece of code at run time? 我想知道有什么方法可以让我们在运行时动态加载一段代码吗? For instance we have a simple "switch...case" statement like this: 例如,我们有一个简单的“ switch ... case”语句,如下所示:

switch($choice) {
   case 'help':
      load_from_db('help');
      break;
   case 'about':
      load_from_db('about');
      break;
}

And we have this database: 我们有这个数据库:

| keyword | command                          |
   help     require('help.php');
   about    echo 'Under construction.';

"load_from_db" is a function which is capable of reading from DB (we know how to do this) and execute corresponding command stored in the database (my question is this part). “ load_from_db”是一个能够从DB读取(我们知道如何做到)并执行存储在数据库中的相应命令的功能(我的问题是这部分)。

Another example would be a simple "textarea" form which user can write some PHP code within and submit the form. 另一个示例是简单的“ textarea”表单,用户可以在其中编写一些PHP代码并提交表单。 At the server side code will be executed and result wil be shown to the user (I know this is not safe, this is just an example). 在服务器端,将执行代码并将结果显示给用户(我知道这并不安全,这只是一个示例)。

Any ideas? 有任何想法吗?

您可以使用,但应始终避免使用http://de.php.net/eval

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM