PHP系统函数eval()使用介绍

什么是eval

eval()函数把字符串按照PHP代码来运算。 该字符串必须是合法的PHP代码,且必须以分号结尾。 如果没有在代码字符串中调用return语句,则返回NULL。如果代码中存在解析错误,则eval()函数返回false

语法

eval(phpcode)

例子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! 

例子2

我们使用for循环创建n个随便,值成倍增加

<?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
分享

TITLE: PHP系统函数eval()使用介绍

LINK: https://www.qttc.net/285-php-eval.html

NOTE: 原创内容,转载请注明出自琼台博客