Limit Analysis on number of nested function layers in PHP

Source: Internet
Author: User

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: Copy codeThe Code is as follows: function rt (){
Static $ I;
Echo $ I ++, '<br/> ';
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:Copy codeThe 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.