什么是eval()?
eval() 函数把字符串按照 php 代码来计算。
该字符串必须是合法的 php 代码,且必须以分号结尾。
如果没有在代码字符串中调用 return 语句,则返回 null。如果代码中存在解析错误,则 eval() 函数返回 false。
语法
eval(phpcode)
参数 描述
phpcode 必需。规定要计算的 php 代码。
例子1
<?php
$string = "beautiful";
$time = "winter";
$str = 'this is a $string $time morning!';
echo $str. "<br />";
eval("\$str = \"$str\";");
echo $str;
?>
输出:
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;
输出:
1
4
9
16
25
36
49
64
81
100
101
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
-
-
-
-
-
-
-
管理后台无限级菜单
管理后台菜单遍历很久就想做一个通用的管理后台出来,一直太懒没做,今天开始粗略搞了一下,只是先把框架搭建好,菜单栏...
[阅读全文]
-
桥接模式学习总结
桥接模式引言假如一个系统需要加入报表模块,主要的开发点是数据的获取和报表的显示方式。数据的获取可能是从系统数据库...
[阅读全文]
-
网友评论