【转】eval 函数用法

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} p {mso-margin-top-alt:auto; margin-right:0cm; mso-margin-bottom-alt:auto; margin-left:0cm; mso-pagination:widow-orphan; font-size:12.0pt; font-family:宋体; mso-bidi-font-family:宋体;} code {mso-ansi-font-size:12.0pt; mso-bidi-font-size:12.0pt; font-family:宋体; mso-ascii-font-family:宋体; mso-fareast-font-family:宋体; mso-hansi-font-family:宋体; mso-bidi-font-family:宋体;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->

PHP 中的eval 函数与javascript 中的同名函数功能相似,都是把一段字符串当作程序代码执行。但是怎么用才让我们感觉到她的真正用处呢?下面是手册的示例代码,也是一般讲这个函数的用法的网页必列的项目:

$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "/n";
eval("/$str = /"$str/";");
echo $str. "/n";
?>
上面这段代码,单引号包括起来的一段字符赋给$str 变量,使得里面的$string$name 变量不起作用。而
eval("/$str = /"$str/";");
这句有点玄,比较难以理解。如果把里面的代码取出来,然后执行echo:
echo "/$str = /"$str/";"
其结果将是:
$str="This is a $string with my $name in it";
所以,eval 函数执行的其实是这段代码。

有了上面的理解,只是了解eval 是怎么工作的,这个鸟函数可以用在什么场合?想上面这段代码,何必要这么麻烦,直接用下面写法,岂不是更简单?( 只需要把单引号变成双引号即可)

$string = 'cup';
$name = 'coffee';
$str = "This is a $string with my $name in it.";
echo $str. "/n";
?>
在手册的eval 函数页,我看到了下面这段:

mixed eval ( string code_str )

eval() evaluates the string given in code_str as PHP code. Among other things, this can be useful for storing code in a database text field for later execution.

我刚好碰到一个使用场合,自定义一些数学公式,放到数据库里面,然后取出来当公式用。如下面代码:

//formula_table
内有formula 字段,一行记录:max($var1,$var2,$var3,$var4);
$sql="select formula from formula_table";
$result=mysql_query($sql);
list($formula)=oci_fetch_row($result);
$formula='$maxvalue='.$formula.';';
$var1=1;
$var2=2;
$var3=3;
$var4=4;
eval($formula);
echo "/$maxvalue=".$max_value; //
输出$maxvalue=4

从手册里面还看到了一些其他用法,抄一些上来,比如给PHP 代码简单加密和解密执行:

echo "/nDECODE nested eval(gzinflate()) by DEBO Jurgen /n/n";
echo "1. Reading coded.txt/n";
$fp1 = fopen ("coded.txt", "r");
$contents = fread ($fp1, filesize ("coded.txt"));
fclose($fp1);
echo "2. Decoding/n";
while (preg_match("/eval/(gzinflate/",$contents)) {
$contents=preg_replace("/</?|/?>/", "", $contents);
eval(preg_replace("/eval/", "/$contents=", $contents));
}
echo "3. Writing decoded.txt/n";
$fp2 = fopen("decoded.txt","w");
fwrite($fp2, trim($contents));
fclose($fp2);
?>

下面是一个读取一个php 文件,然后执行的示例:

$code=file_get_contents("file.php");
$code=str_replace('<'.'?php','<'.'?',$code);
$code='?'.'>'.trim($code).'<'.'?';
eval($code);

好了,希望我的这些理解对你有用。

你可能感兴趣的:(【转】eval 函数用法)