Raf*_*fay 133 php recursion xdebug
我创建了一个函数来查找html文件中的所有URL,并为链接到发现的URL的每个html内容重复相同的过程.该函数是递归的,可以无休止地继续.但是,我通过设置一个全局变量来限制递归,该变量导致递归在100次递归后停止.
但是,php会返回此错误:
致命错误:达到最大功能嵌套级别'100',正在中止!在第1355行的D:\ wamp\www\crawler1\simplehtmldom_1_5\simple_html_dom.php中

我在这里找到了一个解决方案:增加嵌套函数调用限制但这在我的情况下不起作用.
我引用了上面提到的链接中的一个答案.请考虑一下.
"你是否安装了Zend,IonCube或xDebug?如果是这样的话,那可能是你从这里得到这个错误的地方.
几年前我碰到了这个问题,结果是Zend把那个限制放在那里,而不是PHP.当然,删除它会让你超过100次迭代,但最终会达到内存限制."
有没有办法增加PHP中的最大函数嵌套级别
Raf*_*fay 54
一个简单的解决方案解决了我 我刚评论过这一行:
zend_extension = "d:/wamp/bin/php/php5.3.8/zend_ext/php_xdebug-2.1.2-5.3-vc9.dll
Run Code Online (Sandbox Code Playgroud)
在我的php.ini档案中.这个扩展是限制堆栈,100所以我禁用它.递归函数现在正如预期的那样工作.
Lou*_*eau 44
而不是进行递归函数调用,使用队列模型来展平结构.
$queue = array('http://example.com/first/url');
while (count($queue)) {
$url = array_shift($queue);
$queue = array_merge($queue, find_urls($url));
}
function find_urls($url)
{
$urls = array();
// Some logic filling the variable
return $urls;
}
Run Code Online (Sandbox Code Playgroud)
有不同的方法来处理它.如果您需要了解所遍历的原点或路径,您可以跟踪更多信息.还有分布式队列可以使用类似的模型.
小智 41
另一个解决方案是添加xdebug.max_nesting_level = 200你的php.ini
小智 13
进入php.ini配置文件并更改以下行:
xdebug.max_nesting_level=100
Run Code Online (Sandbox Code Playgroud)
类似于:
xdebug.max_nesting_level=200
Run Code Online (Sandbox Code Playgroud)
Guj*_*ana 13
在Ubuntu上使用PHP 5.59:
得到`:
/etc/php5/cli/conf.d
并在该目录中找到你的xdebug.ini,在我的例子中是 20-xdebug.ini
并添加这一行`
xdebug.max_nesting_level = 200
或这个
xdebug.max_nesting_level = -1
将它设置为-1,您不必担心更改嵌套级别的值.
`
van*_*ndf 12
可能是因为xdebug而发生的.
尝试在"php.ini"中注释以下行并重新启动服务器以重新加载PHP.
";xdebug.max_nesting_level"
Lee*_*man 12
尝试查看/etc/php5/conf.d/以查看是否存在名为xdebug.ini的文件
max_nesting_level默认为100
如果未在该文件中设置添加:
xdebug.max_nesting_level=300
Run Code Online (Sandbox Code Playgroud)
到列表末尾所以它看起来像这样
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.profiler_enable=0
xdebug.profiler_enable_trigger=1
xdebug.profiler_output_dir=/home/drupalpro/websites/logs/profiler
xdebug.max_nesting_level=300
Run Code Online (Sandbox Code Playgroud)
然后,您可以在进行此更改之前和之后使用@ Andrey的测试以查看是否有效.
php -r 'function foo() { static $x = 1; echo "foo ", $x++, "\n"; foo(); } foo();'
Run Code Online (Sandbox Code Playgroud)
php.ini中:
xdebug.max_nesting_level = -1
我不完全确定该值是否会溢出并达到-1,但它永远不会达到-1,或者它会将max_nesting_level设置得相当高.
您可以尝试通过实现并行工作(例如在集群计算中)来摆动嵌套,而不是增加嵌套函数调用的数量.
例如:您定义有限数量的插槽(例如100)并监控分配给每个/部分插槽的"工作人员"的数量.如果任何插槽空闲,您将等待的工作人员"放入其中".
从命令行检查递归:
php -r 'function foo() { static $x = 1; echo "foo ", $x++, "\n"; foo(); } foo();'
Run Code Online (Sandbox Code Playgroud)
如果结果> 100 THEN,则检查内存限制;
<?php
ini_set('xdebug.max_nesting_level', 9999);
... your code ...
Run Code Online (Sandbox Code Playgroud)
PS 将 9999 更改为您想要的任何数字。
| 归档时间: |
|
| 查看次数: |
259143 次 |
| 最近记录: |