<?php
set_time_limit(4);
echo ini_get('max_execution_time'); // give value as 4
while ($i<=10)
{
echo "i=$i ";
sleep(1);
$i++;
}
输出:4i = i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9 i = 10
预期行为:它应该在4秒后结束执行。
请解释一下?
答案 0 :(得分:1)
您使用的是哪个版本?
我使用版本 PHP版本5.4.7 ,我得到了结果
4i=0 i=1 i=2 i=3 i=4
Fatal error: Maximum execution time of 4 seconds exceeded in .......
同时在设置 set_time_limit()时,执行时>>中睡眠()的持续时间将 > STRONG>。以下说明:
<?php
set_time_limit(20);
while ($i<=10)
{
echo "i=$i ";
sleep(100);
$i++;
}
?>
Output:
i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9 i=10
的来源
答案 1 :(得分:1)
您应该尝试这样做,只需要一个睡眠时间超过最长执行时间的脚本。
sleep(ini_get('max_execution_time') + 10);
阅读本文:Under Linux, sleeping time is ignored, but under Windows, it counts as execution time.
这是您获取计算时间和系统调用时间的方法。
// Script start
$rustart = getrusage();
// Code ...
// Script end
function rutime($ru, $rus, $index) {
return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
- ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}
$ru = getrusage();
echo "This process used " . rutime($ru, $rustart, "utime") .
" ms for its computations\n";
echo "It spent " . rutime($ru, $rustart, "stime") .
" ms in system calls\n";