php function eval () Introduction and use example, 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 no return statement is called in the code string, NULL is returned. If there is a parsing error in the code, the eval () function returns FALSE.
Grammar
Eval (Phpcode)
Copy the Code code as follows:
Parameter description
Phpcode required. Specifies the PHP code to be computed.
Example 1
Copy the Code code 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 a For loop to create n random, and the value multiplies
Copy the 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. '. \ '
\' ;');
}
Echo '
';
Echo $a 1 + $a 10;
Output:
Copy the Code code as follows:
1
4
9
16
25
36
49
64
81
100
101
How to use the eval () function in PHP
Eval () must be a string in parentheses
If you write $a = 1; Eval ("Echo (\ $a +\ $b);"); Operation result is 1
If it is $a = 1; Eval ("Echo ($a + $b);"); The error
This means it's best to have a strict string and a full PHP statement.
$a = 1; eval ("echo \ $a;");
$a = 1; Eval ("echo $a;");
As a result, the first eval was performed with the Echo $a; And the second sentence is to perform echo 1;
How does PHP use the eval () function to change this instance?
$f = ' $ ';
$f. $a. $this->i
What this is, obviously, should not be the effect you want, which is equivalent to a string connection. Presumably you mean to do this, $ ($a. $this->i). Let's just think about the rest.
http://www.bkjia.com/PHPjc/866677.html www.bkjia.com true http://www.bkjia.com/PHPjc/866677.html techarticle php Function eval () describes and uses the example, eval example what is the eval () eval () function to calculate the string in PHP code. The string must be a valid PHP code, and must be a semicolon ...