PHP eval()Functions
Examples
Evaluate a string as PHP code:
$string = "beautiful";
$time = "winter";
$str = 'This is a $string $time morning!';
echo $str. PHP_EOL;
eval("\$str = \"$str\";");
echo $str;
The above code outputs:
This is a $string $time morning!
This is a beautiful winter morning!
This is a beautiful winter morning!
Definition and Usage
The eval() function evaluates a string as PHP code.
The string must be valid PHP code and must end with a semicolon.
Note:The return statement will immediately terminate the evaluation of the string.
Tip:This function is useful for storing code in database text fields for later evaluation.
Syntax
eval(phpcode)
| Parameter | Description |
|---|---|
| phpcode | Required. Specifies the PHP code to be evaluated. |
Technical Details
| Return value: | Returns NULL unless a return statement is called in the code string, in which case the value passed to the return statement is returned. If there is a parse error in the code string, the eval() function returns FALSE. |
|---|---|
| PHP Version: | 4+ |
PHP Misc Reference Manual Other Extensions