The example in this article describes the method of dynamically executing code in PHP. Share it with everyone for your reference, the details are as follows:
The PHP dynamic execution introduced here means entering the code directly on the page, clicking to execute, and returning the execution results
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 on this site: "Summary of PHP operating office document skills (including word, excel, access, ppt)", "php date Time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation Introductory tutorial " and " Summary of common PHP database operation skills "
I hope this article will be helpful to everyone in PHP programming.