Analysis of the limit of function nesting levels in PHP_PHP tutorial

WBOY
Release: 2016-07-21 15:28:36
Original
846 people have browsed it

Function nesting, this name is a bit confusing and may not be easy to understand. A more common special case of function nesting: recursive functions, that is, functions that nest themselves. I have always thought that there cannot be too many nested functions in PHP. This is because I accidentally used recursion at some point in the past. When the depth of recursion reaches 100, that is, when the number of nested functions reaches 100, the program will report A fatal error. Example as follows:

Copy code The code is as follows:

function rt() {
static $i;
echo $i++, '
';
rt();
}
rt();
die();

in mine The error reported in the win7 + php5.3 environment is as follows: Fatal error: Maximum function nesting level of '100′ reached, aborting!

I always thought it was a limitation of PHP itself, until one day I switched to the liunx environment and ran the command I ran it in row mode and found that the program entered an infinite loop. There are different results in different environments. Why? Well, we looked for the error information directly in the source code and found that there was no relevant content. We directly debugged the entire execution process and there was no error reported under win. What's the reason? Switch to win again, search again, and find the error message in xdebug. Starting at line 1242 of the xdebug.c file:
Copy the code 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 previous limit on the number of layers of function nesting was added by the xdebug extension. Why is there this limit? In xdebug, xdebug will record each function call, including nested function calls, memory in function calls, time and other values. These values ​​are of great use in analyzing program performance. Without this limit, when there are too many nested levels, the machine will run out of memory. If this is a production environment server, then some services will be unavailable. Of course, this extension will not be added in the production environment. But this extension may be available on a development server shared by multiple people. If the machine is unavailable due to a developer's program error, so that all developers cannot work, I think this may be the reason for adding restrictions.

What if we need to increase the number of layers of this restriction? Change the source code and recompile the xdebug extension? No, there is an item in the xdebug configuration item called xdebug.max_nesting_level. By default, this configuration item is commented in php.ini. Remove the comment and change this value to the value you need, 200? If it’s not enough, then 500, but this value should not be too large. If there is too much recursion, it will have a great impact on the performance of the program. At this time, it would be a better solution to implement recursion in the form of a stack or replace it with a loop. , such as: the implementation of Fibonacci sequence (Fibonacci), it will be faster to use loops to implement it.

Conclusion: There is no limit to the function nesting of PHP itself. If there is a limit, it is also a memory limit. This is because PHP's function nesting is implemented in the form of a stack. For each function, a section of memory is allocated to store the local content of the function.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323528.htmlTechArticleFunction nesting, this name is a bit confusing and may not be easy to understand. A more common special case of function nesting: recursive functions, that is, functions that nest themselves. I always thought that in PHP there couldn’t be...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template