1 <?php 2 // http://cn.php.net/manual/zh/function.create-function.php 3 $lambda = create_function('$a,$b','return ($a > $b)? $a : $b;'); 4 var_dump($lambda); 5 echo $lambda(1,2);
下面一些具体的示例
1 function do_foreach($arrData,$func) { 2 $arrResult = array(); 3 foreach($arrData as $row) { 4 if($return = $func($row)) { 5 $arrResult[] = $return; 6 } 7 } 8 return $arrResult; 9 } 10 11 // 所有元素+1 12 $func1 = 'return $row + 1;'; 13 var_dump(do_foreach(array(1,2,3,4),create_function('$row',$func1))); 14 15 // 所有元素平方 16 $func1 = 'return $row * $row;'; 17 var_dump(do_foreach(array(1,2,3,4),create_function('$row',$func1))); 18 19 // 只是输出 20 $func1 = 'echo $row,"<br/>";'; 21 do_foreach(array(1,2,3,4),create_function('$row',$func1));