Home > Backend Development > PHP Tutorial > 解析PHP可变函数的经典用法_PHP

解析PHP可变函数的经典用法_PHP

WBOY
Release: 2016-06-01 12:06:20
Original
835 people have browsed it
复制代码 代码如下:
function map($fun, $list,$params=array()){
    $acc=NULL;
    $last=array_push($params, NULL,$acc)-1;
    foreach($list as $params[$last-1]){     //第一次见这种写法感觉很神奇
        $params[$last]=call_user_func_array($fun , $params  );
    }
    $acc=array_pop($params);
    return $acc;
}
function add($element,$acc){
    if ($acc == NULL);
    return $acc=$element+$acc;
}
$result=0;
$result=addTo($result,1);
$result=addTo($result,2);
$result=addTo($result,3);
echo "result = $result\n";
$result=0;
$result=map('addTo',array(1,2,3));
echo "result= $result\n";
?>

这样以后累加 类似的计算就方便多了。。。
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template