Home  >  Article  >  php教程  >  解析PHP可变函数的经典用法

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

WBOY
WBOYOriginal
2016-06-13 11:47:211049browse

复制代码 代码如下:


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";
?>


这样以后累加 类似的计算就方便多了。。。
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