What is eval ()?
The eval () function calculates the string according to the PHP code.
The string must be a valid PHP code and must end with a semicolon.
Returns NULL if the return statement is not invoked in the code string. If there is a parse error in the code, the eval () function returns FALSE.
Grammar
Eval (Phpcode)
Copy Code code as follows:
Parameter description
Phpcode required. Specify the PHP code to be computed.
Example 1
Copy Code code as follows:
<?php
$string = "Beautiful";
$time = "Winter";
$str = ' 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!
Example 2
We use the For loop to create n random, and the values multiply
Copy Code code as follows:
<?php
For ($i =1 $i <=10; $i + +) {
Eval (' $a '. $i. ' = '. $i * $i).
}
For ($i =1 $i <=10; $i + +) {
Eval (' echo $a '. $i. ' <br/>\ '; ');
}
echo ' <br/> ';
Echo $a 1 + $a 10;
Output:
Copy Code code as follows:
1
4
9
16
25
36
49
64
81
100
101