How to get current recursion level in a PHP function

前端 未结 5 1929
灰色年华
灰色年华 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: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 );
      }
    }
    

提交回复
热议问题