且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 PHP 函数中获取当前递归级别

更新时间:2023-01-04 14:48:28

如果您只是想避免达到 PHP 的 100 级递归限制,那么

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

count(debug_backtrace()); 

应该足够了.否则,您无法选择传递深度参数,尽管 precrement 运算符使其更清晰,如下面的示例所示.

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 );
  }
}