This article mainly introduces the eval () function of PHP and the example of its use. This article provides a special example, which is to use eval nested for execution in a loop. For more information, see
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.
If the return statement is not called in the code string, NULL is returned. If a parsing error exists in the code, the eval () function returns false.
Syntax
Eval (phpcode)
The code is as follows:
Parameter description
Phpcode is required. Specifies the PHP code to be calculated.
Example 1
The code is as follows:
<? Php
$ String = "beautiful ";
$ Time = "winter ";
$ Str = 'This is a $ string $ time morning! ';
Echo $ str ."
";
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 values, and the values increase exponentially.
The code is as follows:
<? Php
For ($ I = 1; $ I <= 10; $ I ++ ){
Eval ('$ a'. $ I.' = '. ($ I * $ I ).';');
}
For ($ I = 1; $ I <= 10; $ I ++ ){
Eval ('echo $ a'. $ I .'.\'
\';');
}
Echo'
';
Echo $ a1 + $ a10;
Output:
The code is as follows:
1
4
9
16
25
36
49
64
81
100
101