Home  >  Article  >  Backend Development  >  [php] php lambda

[php] php lambda

WBOY
WBOYOriginal
2016-06-23 14:29:202928browse

1 <?php2 // http://cn.php.net/manual/zh/function.create-function.php3 $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 // 所有元素+112 $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));

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn