什么是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
查看更多关于PHP系统函数eval()使用教程的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did8181