Tuesday 15 November 2011

set_time_limit in php, set_time_limit in php5, why set_time_limit?

WHY set_time_limit ?
It is used to run the scripts in the file for a particular amount of time. If we just put set_time_limit(1) then the setting will allow the file to run the script for 1 second and if the time exceeds then it will throw the fatal error

Set_time_limit(0) – No time limit will be set, but I didt checked my MAX_EXECUTION_TIME, if there is still problem in the set_time_limit please check with the MAX_EXECUTION_TIME in the php.ini settings.

Let us check some examples
set_time_limit(0);
function ran()
{
$i=100000;
for ($j=0;$j<$i;$j++)
{
echo $j;
}
}
$gg=ran();
?>
>>>>> WONT THROW ANY ERROR


set_time_limit(1);
function ran()
{
$i=100000;
for ($j=0;$j<$i;$j++)
{
echo $j;
}
}
$gg=ran();
?>

>>>>>> HERE WE SET THIS TO 1 SECOND BUT THE SCRIPT NEEDS TO RUN A LOOP FOR 100000 SO IT WILL THROW THE ERROR LIKE BELOW
Fatal error: Maximum execution time of 1 second exceeded in ------------------------------------------------------------------------

No comments:

Post a Comment