Open In App

How is it possible to set an infinite execution time for PHP script ?

Yes, it is possible to set an infinite execution time for the PHP Script. We can do it by adding the set_time_limit() function at the beginning of the PHP script.

The set_time_limit() function takes only one parameter that is int value which is in seconds and it returns a boolean value. If the script reaches the amount of time given, then it gives a fatal error.



Syntax:

set_time_limit ( int $seconds )

Return Value: Boolean ( True or False )



Default value:30 seconds

For the maximum or you can say for the infinite execution time, we can set the function with the 0 value. If the value is 0 then it will run for an infinite time.

Note: This method will only work if you are allowed to change PHP configuration by the Hosting Server.

 Example 1: The following example is of setting the execution time to 40 seconds




<?php
    
    // This will set the time limit to 40 seconds 
    // Default time is 30 seconds
    set_time_limit(40);
    $j=1;
    while($j<5)
    {
       echo "$j, ";
       $j++;
     }
?>

Output:

 1, 2, 3, 4,

Examples 2: The following example is of the setting time to maximum(infinite) time.




<?php
    
    // This will set the time limit to infinite (maximum) 
    // Default time is 30 seconds
    set_time_limit(0);
    $j=1;
    while($j<50){
     echo "$j, ";
     $j++;
   }
?>

Output:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
47, 48, 49,

Article Tags :