Page

[php]解决php30秒超时操作Fatal error: Maximum execution time of 30 s

674Anson16-07-21


Fatal error: Maximum execution time of 30 seconds exceeded in

http://tp0.top/index.php/article/page/id/149.html


之前这篇文章里面求完全数,如果数值范围过大,运行时间超过30秒则报错,今天碰巧找到了解决方法,就是在开头增加set_time_limit函数。


set_time_limit设置允许脚本运行的时间,单位为秒。如果超过了此设置,脚本返回一个致命的错误。默认值为30秒,或者是在php.ini的max_execution_time被定义的值,如果此值存在。


<?php

header("content-type:text/html;charset=utf-8");
 
set_time_limit(0);  //0表示持续连接

    for ($i = 1; $i <= 100000; $i++) {
        for ($j = 1; $j <= $i/2; $j++) {
            if ($i % $j == 0) {
                $arr[] = $j;
            }
        }
        if (isset($arr)) {
            if (array_sum($arr) == $i) {
                $res[$i]['perfect'] = $i;
                $res[$i]['factors'] = $arr;
            }
            $arr = array();
        }
    }
     
    if (isset($res)) {
        foreach($res as $key =>$value) {
            echo $value['perfect'].'--因数有:'.implode(',', $value['factors']).'<br>';
        }
    }else {
        echo '没有完全数';
}

 ?>


来自ansion博客

2016-7-21