The eval () function of PHP is described and used as an eval example. The eval () function of PHP introduces and uses examples. The eval () function calculates the string according to PHP code. This string must be a valid PHP code and must be described and used using the semicolon PHP function eval (). eval example
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
How to use eval () function in PHP
The eval () must be a string
Assume that you write $ a = 1; eval ("echo (\ $ a + \ $ B);"); the running result is 1.
An error is returned if it is $ a = 1; eval ("echo ($ a + $ B );");
This indicates that it is best to use a strict string and a complete PHP statement.
$ A = 1; eval ("echo \ $ ;");
$ A = 1; eval ("echo $ ;");
The above results show that the first eval executes echo $ a, and the second sentence is echo 1;
How does php use the eval () function to change this instance?
$ F = '$ ';
$ F. $ a. $ this-> I
What is this writing method? obviously it should not be the effect you want, which is equivalent to string connection. You probably mean this, $ ($ a. $ this-> I ). Consider other things.
The eval () function calculates the string according to PHP code. The string must be a valid PHP code and must be a semicolon...