Parsing the classic use of PHP variable functions

  • 2020-06-15 07:54:54
  • OfStack


<?php
function map($fun, $list,$params=array()){
    $acc=NULL;
    $last=array_push($params, NULL,$acc)-1;
    foreach($list as $params[$last-1]){     // The first 1 This is a fantastic way to write it 
        $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";
?>

This will make it easier to add up similar calculations in the future...

Related articles: