Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current recursion level in a PHP function

Tags:

php

recursion

How can I get the current recursion level in a PHP function?

I mean, is there any "magical" (or eventually normal) function like the following?

function doSomething($things) {
    if (is_array($things)) {
        foreach ($things as $thing) {
            doSomething($thing);
        }
    } else {
        // This is what I want:
        echo current_recursion_level();
    }
}

I know I can use another function argument ($level in this example):

function doSomething($things, $level = 0) {
    if (is_array($things)) {
        foreach ($things as $thing) {
            $level++;
            doSomething($thing, $level);
        }
    } else {
        echo $level;
    }
}

But I want to know if there is a built-in function (or trick) to do that. Maybe something with debug_backtrace(), but it does not seem to be a simple or quick solution.

I did not found this information. Maybe it simply does not exists...

like image 668
rap-2-h Avatar asked Nov 12 '13 09:11

rap-2-h


People also ask

What is the depth of the recursion?

Abstract. The maximum depth of recursion refers to the number of levels of activation of a procedure which exist during the deepest call of the procedure.

What is the maximum depth of recursion?

The maximum recursion depth in Python is 1000. You can change the limit by calling sys. setrecursionlimit() method. Consider this a dangerous action!

How do you fix maximum recursion depth exceeded?

The “maximum recursion depth exceeded in comparison” error is raised when you try to execute a function that exceeds Python's built in recursion limit. You can fix this error by rewriting your program to use an iterative approach or by increasing the recursion limit in Python.


2 Answers

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 );
  }
}
like image 88
Michael Morris Avatar answered Oct 22 '22 23:10

Michael Morris


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.

like image 1
Fleshgrinder Avatar answered Oct 23 '22 00:10

Fleshgrinder