How to get current recursion level in a PHP function

前端 未结 5 1887
灰色年华
灰色年华 2021-01-18 05:20

How to get current recursion level in a PHP function ? I mean, is there any \"magical\" (or eventually normal) function like this :

function doSomething($thi         


        
相关标签:
5条回答
  • 2021-01-18 05:31

    You need to count it yourself, the only alternative would be something like XDebug which profiles your complete software. But this is highly inefficient.

    <?php
    
    function my_recursive_fn($param) {
        static $counter = 0;
        if (is_array($param)) {
            ++$counter;
            foreach ($param as $k => $v) {
    
            }
        }
        else {
            echo $counter;
            --$counter;
    
            // If we're returning (only PHP 5.5+)
            try {
                return $counter;
            }
            finally {
                --$counter;
            }
        }
    }
    
    ?>
    

    This allows you to count without a second public parameter.

    0 讨论(0)
  • 2021-01-18 05:36

    In java you can inspect the call stack. I think you can do the same in php:

    debug-backtrace Is this the one you are looking for?

    Since php does not optimize recursion with tail calls this should tel you the depth of the recursion.

    0 讨论(0)
  • 2021-01-18 05:39
        function doSomething($things) {
    static $level = 0;
        if (is_array($things)) {
            foreach ($things as $thing) {
    $level++;
                doSomething($thing);
            }
        } else {
            // This is what I want :
            echo $level
        }
    }
    
    0 讨论(0)
  • 2021-01-18 05:50
    private function select($modules, $level = 0)
    {
        $return_html = '';
        $level_html = '';
        $new_level = 0;
        
        foreach($modules as $module)
        {
            $repeat = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $level);
            
            if(!empty($module['children']))
            {
                $return_html .= '<option>'.$repeat.' '.$module['title'].'</option>';
                
                $new_level = $level+1;
                
                $return_html .= $this->select($module['children'], $new_level);
            }
            else
            {
                
                $return_html .= '<option>'.$repeat.' '.$module['title'].'</option>';
            }
        }
        
        return $return_html;
    }
    
    0 讨论(0)
  • 2021-01-18 05:52

    If you are just looking to avoid hitting PHP's 100 level recursion limit then

    count(debug_backtrace()); 
    

    should be sufficient. Otherwise you've no choice to pass a depth argument, though the precrement operator makes it somewhat cleaner as seen in the example below.

    function recursable ( $depth = 0 ) {
      if ($depth > 3) {
        debug_print_backtrace();
        return true;
      } else {
        return recursable( ++$depth );
      }
    }
    
    0 讨论(0)
提交回复
热议问题