Difference between set_time_limit() and ini_set('max_execution_time', ...)

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.)


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() returns FALSE on failure and does not trigger warnings.

In practice, it should not be a great deal since safe mode is 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.