I have a function that I know takes a long time to execute. Thus, I want to do this at the beginning of the function:
set_time_limit(0);
When I am done with the function, will time limit be returned to whatever it was originally? Or should I set it back to what it was originally? If so, how do I get the original max execution time?
You can use http://www.php.net/manual/en/function.get-cfg-var.php or a class like this:
class TempIniValues {
private $original_settings = array();
public function __construct($var_name, $value) {
$this->set($var_name, $value);
}
public function set($var_name, $value) {
$this->original_settings[$var_name] = ini_get($var_name);
ini_set($var_name, $value);
}
public function get($var_name, $if_orig=false) {
return $if_orig?$this->get_orig($var_name):ini_get($var_name);
}
public function get_orig($var_name) {
return isset($this->original_settings[$var_name])?$this->original_settings[$var_name]:NULL;
}
public function __destruct() {
foreach($this->original_settings AS $setting_name => $setting_value) {
ini_set($setting_name,$setting_value);
}
}
}
function test() {
var_dump(ini_get('max_execution_time'));
$temp_ini=new TempIniValues('max_execution_time',42);
var_dump(ini_get('max_execution_time'));
}
var_dump(ini_get('max_execution_time'));
test();
var_dump(ini_get('max_execution_time'));
The value won't reset when leaving the function. You could read the original using ini_get, but it's non trivial to get the right behaviour, note the following from http://php.net/set_time_limit :
When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.