Treat strings as PHP code:
<?php $string = " beautiful " ; $time = " winter " ; $str = ' This is a $string $time morning! ' ; echo $str . PHP_EOL ; eval ( " $ str = " $str " ; " ) ; echo $str ; ?>The output result of executing the above code is:
This is a $string $time morning!This is a beautiful winter morning!The eval() function evaluates the string according to the PHP code.
The string must be valid PHP code and must end with a semicolon.
Note: The return statement immediately terminates evaluation of the string.
Tip: This function is useful for storing code in a database text field for later calculation.
eval( phpcode )
parameter | describe |
---|---|
phpcode | Required. Specifies the PHP code to be calculated. |
Return value: | The value passed to the return statement is returned unless the return statement is called in a code string, otherwise NULL is returned. If there is a parsing error in the code string, the eval() function returns FALSE. |
---|---|
PHP version: | 4+ |