php


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


Is there any actual difference between these two lines of code?
ini_set('max_execution_time', 20*60);
set_time_limit(20*60);
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.
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';
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.
See http://php.net/manual/en/function.set-time-limit.php for more information.
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.
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).
Both modes "set_time_limit(5)" and "ini_set('max_execution_time', '5')" reset time, a practical and clear example:
//-----------------------------------------------------------
//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
That code only is true on windows.
Sleep time in php linux don't consume execution time
for example in 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
but the same code on Windows with default configuration will show
1 | 2

Related Links

How to read data from nth sheet of Excel sheet in php?
Ad Preview of All Facebook Ads under an account
How to enable php curl extension
How load a table into multiple webpages using HTML?
Woo commerce how to display child category titles along with the parent one?
Active Collab SSL related error
Codeception\Exception\TestParseException Couldn't parse test
php preg_match not working as in other web applications
When try to reindexing in magento2 by ubuntu terminal I am getting this(Access denied for user 'root'#'localhost'(using password: NO)) error
Datatables and slow Ajax sourced data
Laravel Yajra datatables headers label not working
Zend Framework 2 Checkbox setValue for unchecked not work
how to define an access for a member of telegram bot?
Adding product using WooCommerce Rest Api - Stock issue
checking urls against tables in the database
PHP Warning: Module 'ssh2' already loaded in Unknown on line 0

Categories

HOME
user-interface
magento2
xcode8.1
d3.js
windows-10
message-queue
hdfs
visual-studio-2010
typeerror
bing-search
biztalk-2010
video-streaming
gprs
sympy
specifications
swarm
parsley.js
chromecast
grocery-crud
dhcp
string-formatting
tfsbuild
clr
bsd
web-analytics
maven-surefire-plugin
liferay-6.2
qa
ini
plyr
cellular-network
sap-lumira
pyspark-sql
cube
docx4j
delayed-job
shapeless
sql-execution-plan
apptentive
jfxtras
des
textmate2
subclassing
mars-simulator
django-smart-selects
extbase
export-to-pdf
vb.net-to-c#
icepdf
angular-datatables
minikube
contactless-smartcard
notesview
reverse-dns
moinmoin
xcode8.2
axis
qvtkwidget
ssi
webalizer
sony-future-lab-n
nonlinear-functions
punctuation
android-sharing
asp.net-web-api-routing
nrf51
gawk
annotatorjs
haraka
metabase
zynq
boost-proto
textpattern
variable-length-array
arbre
place
unhandled
postal-code
ngcordova
compositetype
modern.ie
domino-designer-eclipse
algebraixlib
physicsjs
webproject
kognitio-wx2
back
accessory
screwturn
prism.js
redpitaya
cpu-time
usb-flash-drive
profiles
smips
void-pointers
flv
notin
delphi-xe4
chefspec
significant-digits
audiotoolbox
wndproc
mkannotation
gwt-celltable
qtextbrowser
snackjs
page-curl
canonicalization
ninject-interception
yahoo-maps
inversion

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App