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...