The example in this article describes how PHP implements dynamic code execution. Share it with everyone for your reference, the details are as follows:
The PHP dynamic execution introduced here means that you directly enter the code on the page, click to execute, and the execution result is returned
The method is very simple, mainly using:
$newfunc = create_function('', $code);
function to implement.
The code is as follows:
<?php $code = 'return "no code!";'; if (isset($_POST['code']) && $_POST['code'] != '') { $code = $_POST['code']; } $newfunc = create_function('', $code); $res = $newfunc(); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>XXX</title> </head> <body> <form action="run.php" method="POST"> <textarea name="code" style="width:100%; height:300px;"><?php echo $code ?></textarea><br> <input type="submit" value="RUN" /> </form> <hr> <div><?php echo $res ?></div> </body> </html>
Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage", "php-oriented "Introduction Tutorial on Object Programming", "Summary of PHP String Usage", "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"
I hope this article will be helpful to everyone in PHP programming.