Function inside a function.?

前端 未结 6 1840
野性不改
野性不改 2020-11-29 03:02

This code produces the result as 56.

function x ($y) {
    function y ($z) {
        return ($z*2);
    }

    return($y+3);
}

$y = 4;
$y = x($y)*y($y);
ech         


        
6条回答
  •  渐次进展
    2020-11-29 03:54

    This is useful concept for recursion without static properties , reference etc:

    function getRecursiveItems($id){
        $allItems = array();
        
        function getItems($parent_id){
           return DB::findAll()->where('`parent_id` = $parent_id');
        } 
       
        foreach(getItems($id) as $item){
             $allItems = array_merge($allItems, getItems($item->id) );
        }
    
        return $allItems;
    }
    

提交回复
热议问题