PHP的最大递归层数

来源:互联网 发布:淘宝店买什么好 编辑:程序博客网 时间:2024/06/07 07:44

PHP的最大递归层数跟程序内存限额有关。php5默认允许一个程序使用128M的内存,因此当你的递归层数过大导致128M内存耗尽时,程序会产生一个致命错误并退出。

 

编辑php.ini文件可以更改PHP的最大内存使用限制:

; Maximumamount of memory a script may consume (128MB)

; http://php.net/memory-limit

memory_limit= 128M

 

 

下边的小实验可以验证php在128M内存配额下运行一个简单递归程序时,递归层级最高可达到38万层:

<?php

 

        function re($level)

        {

                printf("now is level%d\n",$level);

                $level++;

                re($level);

                printf("level %d isout\n",$level);

        }

 

        re(1);

 

执行后输出:

now is level380118

now is level380119

now is level380120

PHP Fatalerror:  Allowed memory size of 134217728bytes exhausted (tried to allocate 130968 bytes) in /root/envir/recusion.php online 8

阅读全文
0 0