PHP Function eval of Introduction and Use Examples

  • 2021-07-13 04:51:30
  • OfStack

What is eval ()?

The eval () function evaluates 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 there is a parsing error in the code, the eval () function returns false.

Grammar

eval(phpcode)


Parameter   Describe
phpcode  Necessary. What is stipulated to be calculated PHP Code.

Example 1


<?php
$string = "beautiful";
$time = "winter";
 
$str = 'This 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 casually, and the value is multiplied


<?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 $a1 + $a10;

Output:

1
4
9
16
25
36
49
64
81
100
101


Related articles: