function nesting layer limit analysis _php techniques in PHP

Source: Internet
Author: User
function nesting, the name is a bit tangled, perhaps not too good to understand. A more common special case of function nesting: recursive functions, that is, the function itself nesting itself. Always thought in PHP can not have too many functions nesting, this is because at some time before the use of recursion, in the recursive depth of 100, that is, the function nesting layer number 100, the program will report a Fatal error. The following example:
Copy Code code as follows:

function rt () {
static $i;
echo $i + +, ' <br/> ';
RT ();
}
RT ();
Die ();

In my win7 + php5.3 environment, the error is as follows: Fatal error:maximum function nesting level of ' 100′reached, aborting!

Always thought is the limit of PHP itself, until one day to switch to the LIUNX environment in order to run the mode, found that the program restricted to the dead loop. Different circumstances have different results, why? OK, we directly in the source code to find the error information, found no relevant content, directly debug the entire implementation process, there is no error under win. What's the reason? Switch to win again, find again, found in the Xdebug to see the error message. Start at 1242 lines of the xdebug.c file:
Copy Code code 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 that mean? Why is there a limit to the number of Xdebug extensions added to the previous function nesting? In Xdebug, Xdebug records every function call, including nested function calls, memory in function calls, and time equivalents, which can be used to analyze program performance. Without this restriction, the machine will run out of memory when there are too many layers of nesting. If this is a production environment server, then some of the services will not be available, of course, the production environment will not add this extension. But this extension may be available on a multiuser development server, and if the machine is not available because of a developer's bug, so that all developers are not able to work, I think that might be the reason for adding restrictions.

What if we need to increase the number of layers in this limit? Change source code, recompile xdebug extension? No, there is one item in the Xdebug configuration item called Xdebug.max_nesting_level, by default, the configuration item in PHP.ini is annotated, the annotation is removed, and the value is the value you want, 200? Not enough, that 500, but this value is not too big, if recursion is too much, the performance of the program has a great impact, at this point, the form of a stack to achieve recursion or to replace recursion with a loop is a better solution, such as: the Fibonacci sequence (Fibonacci) implementation, with the loop to achieve faster.

Conclusion: There is no limit to the function nesting of PHP itself, if there are limitations, it is also the limit of memory. This is because PHP's function nesting is implemented in the form of stacks. For each function, a section of memory is allocated to store the contents of the function part.

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.