PHP eval() function
Definition and Usage
The eval() function calculates the string as PHP code.
The string must be valid PHP code and must end with a semicolon.
If the return statement is not called in the code string, it returns NULL. If there is a parsing error in the code, the eval() function returns false.
Syntax
eval(phpcode)
Parameter | Description |
---|---|
phpcode | Required. Specifies the PHP code to be calculated. |
Tips and Notes
Note:The return statement will immediately terminate the calculation of the string.
Note:This function is very useful for storing code in database text fields for future calculations.
Example
<?php $string = "beautiful"; $time = "winter"; $str = 'This is a $string $time morning!'; echo $str. "<br />"; eval("\$str = \"$str\";"); echo $str; ?>
Output:
This is a $string $time morning! This is a beautiful winter morning!