这两行代码之间是否有任何实际差异?
ini_set('max_execution_time', 20*60);
set_time_limit(20*60);
Run Code Online (Sandbox Code Playgroud)
mar*_*rio 35
看目前的来源:
/* {{{ 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);
}
/* }}} */
Run Code Online (Sandbox Code Playgroud)
set_time_limit()确实只是一个围绕着这个ini_set()电话的便利包装.它似乎甚至没有执行广告计时器重置.(但我猜"计时器"实际上不是一个单独的实体,但是ini值本身就是这样使用的.)
Álv*_*lez 32
要考虑的一个微小差异是它们在失败时的行为方式:
set_time_limit()不会返回任何内容,因此您无法使用它来检测它是否成功.此外,它会发出警告:
警告:set_time_limit():无法在安全模式下设置时间限制
ini_set()FALSE失败时返回并且不会触发警告.
在实践中,它应该不是很大,因为据称安全模式是唯一可能导致失败并且该功能已被弃用的情况.
除此之外,该函数只是属性更改的包装器.
web*_*ave 16
不,没有.
echo ini_get('max_execution_time'); // 30
set_time_limit(100);
echo ini_get('max_execution_time'); // 100
Run Code Online (Sandbox Code Playgroud)
关于定时器复位,在两种情况下都会重置:
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';
Run Code Online (Sandbox Code Playgroud)