How is it possible to set an infinite execution time for PHP script ?
Last Updated :
19 Jul, 2021
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
<?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
<?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,
Similar Reads
How to increase execution time of a PHP script ? Increasing the execution time of a PHP script means extending the maximum time a script is allowed to run before the server terminates it. This is often necessary for resource-intensive tasks, like data processing or large file uploads, to prevent timeouts.Here we have some common approaches to incr
2 min read
How to Measure Script Execution Time in PHP? Measuring the script execution time in PHP is the time required to execute the PHP script. To calculate script execution time use clock time rather than the CPU execution time. Measuring script execution time is useful to check and improve the execution time for better application performance.Measur
2 min read
How to Measure Script Execution Time in PHP? Measuring the script execution time in PHP is the time required to execute the PHP script. To calculate script execution time use clock time rather than the CPU execution time. Measuring script execution time is useful to check and improve the execution time for better application performance.Measur
2 min read
How to terminate execution of a script in PHP ? In this article, we are going to discuss how to terminate the execution of a PHP script. In PHP, a coder can terminate the execution of a script by specifying a method/function called the exit() method in a script. Even if the exit() function is called, the shutdown functions and object destructors
2 min read
How to set Maximum Execution Time for a Promise Await in JavaScript ? In JavaScript, asynchronous operations are more common, often implemented using promises and async/await syntax. However, there are certain scenarios where we want to set the execution times for these asynchronous operations to prevent them from running indefinitely. In this article, we'll explore v
3 min read
How to Destroy Session After Some Time in PHP ? In PHP, we create sessions for the user who is logged in and make that user online till the user log out of that session. It can be done by clicking on the logout button or by destroying that session after a fixed time. By default the expiry time of any particular session that is created is 1440 se
3 min read