设置 php max_execution_time 的正确方法

Proper way to set php max_execution_time

我尝试将我的执行时间设置为 php,但似乎不起作用。

我已经试过了。

<?php

function secondsToTime($s)
{
    $h = floor($s / 3600);
    $s -= $h * 3600;
    $m = floor($s / 60);
    $s -= $m * 60;
    return $h.':'.sprintf('%02d', $m).':'.sprintf('%02d', $s);
}
$a = microtime(true);

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

ini_set('max_execution_time', 3);
sleep(2);
echo 'oke';

sleep(3);
echo 'yes';
$b = microtime(true);

echo secondsToTime($b-$a);

?>

但是当我尝试时,它显示 11 秒,而不是错误或其他东西。

为什么我的代码没有错误??我将时间设置为 6 秒。但我的整个过程是 11 秒?? 我是否也需要在 php.ini 上定义它? 谢谢。

set_time_limit 以秒为单位设置最大执行时间。如果设置为零,则没有时间限制

set_time_limit(0);

By default, the maximum execution time for PHP scripts is set to 30 seconds. If a script runs for longer than 30 seconds, PHP stops the script and reports an error. You can control the amount of time PHP allows scripts to run by changing the max_execution_time directive in your php.ini file.

显示这个错误

Fatal error: Maximum execution time of 6 seconds exceeded

您不会收到此错误,因为您的服务器 "Display Error" 未开启。

我也需要在我的 php.ini 上定义它吗?

无需在 php.ini 上定义它 too.you 可以像您已经拥有的那样动态设置它。