There is no limit on function nesting in PHP. if there is a limit, it is also a limit on memory. This is because function nesting in PHP is implemented in the form of stacks. Each function is allocated with a memory segment to store partial content of the function. The name of function nesting is a bit tangled and may not be easy to understand. A common case of function nesting: recursive function, that is, the function itself nest itself. I always thought that there should not be too many function nesting in PHP. this is because recursion was accidentally used in the past. when the depth of recursion reaches 100, when the number of nested function layers reaches 100, the program reports a Fatal error. Example:
The code is as follows:
Function rt (){
Static $ I;
Echo $ I ++ ,'
';
Rt ();
}
Rt ();
Die ();
In my win7 + php5.3 environment, the following error is reported: Fatal error: Maximum function nesting level of '000000' reached, aborting!
I always thought it was a limitation of PHP, until one day I switched to the liunx environment and ran it in command line mode. I found that the program was limited to an endless loop. Different environments have different results. why? Well, we can directly find the error information in the source code and find that there is no relevant content. we can directly debug the entire execution process without reporting the error in win. Why? Switch to win again, search again, and find the error message in xdebug. Start at Line 1 of the xdebug. c file:
The code is as follows:
XG (level) ++;
If (XG (level) = XG (max_nesting_level )){
Php_error (E_ERROR, "Maximum function nesting level of '% ld' reached,
Aborting! ", XG (max_nesting_level ));
}
What does this mean? The number of layers of nested functions is limited by the xdebug extension. why is this limit? In xdebug, xdebug records each function call, including nested function calls, memory in function calls, and time equivalence. these values are useful in analyzing program performance. Without this restriction, the machine runs out of memory when there are too many layers of nesting. If this is a production environment server, some services will be unavailable. of course, this extension will not be added in the production environment. However, this extension may be available on a development server shared by multiple developers. if the machine becomes unavailable due to a program error of a developer, all developers will not be able to work, I think this may be the reason for adding restrictions.
What if we need to increase the number of layers of this limit? Change the source code and re-compile the xdebug extension? No. there is a configuration item named xdebug in xdebug. max_nesting_level. by default. in ini, this configuration item is commented out. remove the comment and set this value to the value you need, 200? Not enough. let's say 500, but the value should not be too large. if there are too many recursion operations, it will have a great impact on the program performance. at this time, implementing recursion in the form of stacks or replacing recursion with loops is a better solution. for example, the implementation of the Fibonacci series (Fibonacci) is faster.
Conclusion: there is no limit on function nesting in PHP. if there is a limit, it is also a limit on memory. This is because function nesting in PHP is implemented in the form of stacks. Each function is allocated with a memory segment to store partial content of the function.