Home > Article > Backend Development > Introduction and usage examples of PHP function eval(), eval example_PHP tutorial
What is eval()?
The eval() function evaluates strings according to PHP code.
The string must be valid PHP code and must end with a semicolon.
If no return statement is called in the code string, NULL is returned. If there are parsing errors in the code, the eval() function returns false.
Grammar
eval(phpcode)
Example 1
Output:
This is a $string $time morning!
This is a beautiful morning winter!
Example 2
We use a for loop to create n random numbers, and the values are doubled
eval() must be a string in brackets
If you write $a=1; eval("echo (\$a+\$b);"); the running result is 1
If $a=1; eval("echo ($a+$b);"); then an error will be reported
This means that it is best to have a strict string and a complete PHP statement
$a=1; eval("echo \$a;");
$a=1; eval("echo $a;");
The above result is the same as the first eval is executed echo $a; and the second sentence executes echo 1;
$f ='$';
$f.$a.$this->i
What is the way to write this? Obviously it is not the effect you want. This is equivalent to a string Connected. Probably what you mean is this, $($a.$this->i). Let’s think about the other things slowly