php eval函数用法
eval
将值代入字符串之中。
语法: void eval(string code_str);
传回值: 无
函式种类: 数据处理
内容说明
本函式可将字符串之中的变量值代入,通常用在处理数据库的数据上。参数 code_str 为欲处理的字符串。值得注意的是待处理的字符串要符合 PHP 的字符串格式,同时在结尾处要有分号。使用本函式处理后的字符串会沿续到 PHP 程序结束。
范例:
<?php
$string = ‘cup’;
$name = ‘coffee’;
$str = ‘This is a $string with my $name in it.’;
echo $str. “\n”;
eval(”\$str = \”$str\”;”);
echo $str. “\n”;
?>
The above example will show:
This is a $string with my $name in it.
This is a cup with my coffee in it.
这里我们再举一个处理数据库的例子,例子如下:
我们需要向add_col传送一系列参数,其中包括一个return_area_name函数。
$acount_list->add_col(’所属区域’,'area_id’,'db’,0,’return_area_name($rs[area_id])’);
return_area_name函数定义如下:
function return_area_name($area_id)
{
global $db;
$areaName = $db->get_var(”select AREA_NAME from 数据表 where id=$area_id”);
return “$areaName”;//此处需要使用双引号
}
将一系列参数传至add_col函数内,add_col函数是某类中的一个方法,定义如下:
public function add_col($alias,$field,$data_type,$td_width,$bind_code)
{
$col = new stdClass();//关于stdClass的用法我们下节再讲
$col->alias=$alias;
$col->name=$field;
$col->data_type=$data_type;
$col->bind_code=$bind_code;
$col->width=$td_width;
$this->columns[$field]=$col;
}
我们可以看到带有函数的参数是传至$col实例方法的bind_code变量,此处有个bind_code方法负责对齐转移:
$colstr=$this->bind_code($col->bind_code,$rs);
bind_code函数定义如下:
private function bind_code($code,$rs)
{
if($code!=”)
{
$code=’$o=’.$code.’;';
eval($code);
return $o;
}
else
return false;
}
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments
还没有评论。
发表评论