php set_time_limit() 和 ini_set('max_execution_time', ...) 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8914257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between set_time_limit() and ini_set('max_execution_time', ...)
提问by álvaro González
Is there any actual difference between these two lines of code?
这两行代码之间有什么实际区别吗?
ini_set('max_execution_time', 20*60);
set_time_limit(20*60);
采纳答案by mario
Looking at the current source:
查看当前来源:
/* {{{ proto bool set_time_limit(int seconds)
Sets the maximum time a script can run */
PHP_FUNCTION(set_time_limit)
{
zend_long new_timeout;
char *new_timeout_str;
int new_timeout_strlen;
zend_string *key;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &new_timeout) == FAILURE) {
return;
}
new_timeout_strlen = zend_spprintf(&new_timeout_str, 0, ZEND_LONG_FMT, new_timeout);
key = zend_string_init("max_execution_time", sizeof("max_execution_time")-1, 0);
if (zend_alter_ini_entry_chars_ex(key, new_timeout_str, new_timeout_strlen, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC) == SUCCESS) {
RETVAL_TRUE;
} else {
RETVAL_FALSE;
}
zend_string_release(key);
efree(new_timeout_str);
}
/* }}} */
set_time_limit()
is indeed just a convenience wrapper around the according ini_set()
call. It doesn't even seem to perform the advertised timer reset. (But I would guess the "timer" actually isn't a separate entity, but the ini value itself is used as such.)
set_time_limit()
确实只是相应ini_set()
调用的方便包装。它甚至似乎没有执行广告定时器重置。(但我猜“计时器”实际上不是一个单独的实体,但 ini 值本身就是这样使用的。)
回答by álvaro González
A tiny difference to take into account is the way they behave on failure:
需要考虑的一个微小差异是它们在失败时的行为方式:
set_time_limit()
does not return anything so you can't use it to detect whether it succeeded. Additionally, it'll throw a warning:Warning: set_time_limit(): Cannot set time limit in safe mode
ini_set()
returnsFALSE
on failure and does not trigger warnings.
set_time_limit()
不返回任何内容,因此您无法使用它来检测它是否成功。此外,它会发出警告:警告:set_time_limit():无法在安全模式下设置时间限制
ini_set()
FALSE
失败时返回并且不会触发警告。
In practice, it should not be a great deal since safe modeis allegedly the only situation that can cause a failure and the feature is already deprecated.
实际上,这应该不是什么大问题,因为据称安全模式是唯一可能导致失败的情况,并且该功能已被弃用。
Other than that, the function is just a wrapper for the property change.
除此之外,该函数只是属性更改的包装器。
回答by webbiedave
No there isn't.
不,没有。
echo ini_get('max_execution_time'); // 30
set_time_limit(100);
echo ini_get('max_execution_time'); // 100
Regarding timer reset, it is reset in both cases:
关于定时器重置,在两种情况下都会重置:
ini_set('max_execution_time', 10);
for ($i=0; $i<50000000; $i++) {
}
ini_set('max_execution_time', 10); // timer is reset, just as it would be with set_time_limit
for ($i=0; $i<50000000; $i++) {
}
echo 'done';
回答by Chris Browne
According to the php manual, set_time_limit() will reset the execution timer when called. I don't believe ini_set() has the same side-effect, which would be the difference between the two.
根据 php 手册, set_time_limit() 将在调用时重置执行计时器。我不相信 ini_set() 具有相同的副作用,这将是两者之间的区别。
See http://php.net/manual/en/function.set-time-limit.phpfor more information.
有关更多信息,请参阅http://php.net/manual/en/function.set-time-limit.php。
Update: since examining various portions of the php source code (including that referenced by mario's answer), it is my conclusion that ini_set() and set_time_limit() are precisely equivalent.
更新:由于检查了 php 源代码的各个部分(包括 mario 的答案引用的部分),我的结论是 ini_set() 和 set_time_limit() 完全等效。
ini_set() does indeed reset the timer (though I'm still at a loss as to how either function performs the reset, I would have to look up the function that kills the script when the timer ends to figure that one out).
ini_set() 确实重置了计时器(尽管我仍然不知道这两个函数如何执行重置,但我必须查找在计时器结束时杀死脚本的函数才能弄清楚)。
回答by Fernando
Both modes "set_time_limit(5)" and "ini_set('max_execution_time', '5')" reset time, a practical and clear example:
两种模式“set_time_limit(5)”和“ini_set('max_execution_time', '5')”复位时间,一个实用清晰的例子:
//-----------------------------------------------------------
//test "max_execution_time":
ini_set('max_execution_time', 5);
for ($i=0; $i<3; $i++) {
sleep(1);
}
ini_set('max_execution_time', 5);
for ($i=0; $i<3; $i++) {
sleep(1);
}
echo '<br/>';
echo 'done with max_execution_time';
//-----------------------------------------------------------
//test "set_time_limit":
set_time_limit(5);
for ($i=0; $i<3; $i++) {
sleep(1);
}
set_time_limit(5);
for ($i=0; $i<3; $i++) {
sleep(1);
}
echo '<br/>';
echo 'done with set_time_limit';
All "for" complete successfully, this indicates that the time was reset in all cases, Greetings
所有“for”成功完成,这表明在所有情况下时间都被重置,问候
That code only is true on windows. Sleep time in php linux don't consume execution time for example in linux:
该代码仅适用于 Windows。php linux 中的睡眠时间不消耗执行时间,例如在 linux 中:
<?php
set_time_limit(2);
for($i=0; $i<10; $i++)
{
echo ("$i \n");
sleep(1);
}
` will show
` 会显示
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
but the same code on Windows with default configuration will show
但在 Windows 上使用默认配置的相同代码将显示
1 | 2
1 | 2