Current Location: Home> Latest Articles> Introduction to the Basic Usage of the set_time_limit Function with Practical Examples

Introduction to the Basic Usage of the set_time_limit Function with Practical Examples

gitbox 2025-06-15

In PHP programming, script execution time is limited. By default, the script runs for about 30 seconds. If the execution time exceeds this limit, PHP automatically stops the script and throws a timeout error. To prevent this, PHP offers the set_time_limit() function, which allows us to manually set the maximum execution time of a script, ensuring that long-running tasks can complete smoothly.


1. What is set_time_limit?

set_time_limit() is a built-in PHP function used to set the maximum number of seconds a script is allowed to run. Once called, PHP resets the timer and starts counting from the moment of the call.

The function is defined as follows:

set_time_limit(int $seconds): bool
  • The $seconds parameter specifies the allowed script running time in seconds. Setting it to 0 means unlimited time, so the script will not be terminated due to execution time limits.

  • The return value is a boolean: true on success, and false on failure.


2. Functions and Considerations of set_time_limit

  • Resets the timer: Each call resets the maximum execution time counter for the script.

  • Only effective outside safe mode: If PHP runs in safe mode, set_time_limit() has no effect.

  • CLI mode: In command line mode, there is usually no time limit, so set_time_limit() will not affect execution.

  • Works alongside max_execution_time in PHP.ini: The default maximum execution time is 30 seconds, but set_time_limit() can override this.


3. Practical Examples Explained

Here are some examples demonstrating common uses of set_time_limit().

Example 1: Extending Script Execution Time

Suppose you need to run a task that takes 60 seconds, which would normally timeout at 30 seconds:

<?php
// Set the maximum execution time to 90 seconds
set_time_limit(90);
<p>echo "Starting the time-consuming task...\n";</p>
<p>for ($i = 0; $i < 60; $i++) {<br>
sleep(1); // Simulate a time-consuming operation<br>
echo "Executed {$i} seconds\n";<br>
}</p>
<p>echo "Task completed.\n";<br>
?><br>

In this example, calling set_time_limit(90) allows the script to run for up to 90 seconds, preventing it from being terminated at the default 30 seconds.

Example 2: Unlimited Execution

For some tasks with uncertain execution time that should not be limited by timeouts:

<?php
// Remove execution time limit
set_time_limit(0);
<p>while (true) {<br>
// Code that needs to run continuously<br>
echo "Script running indefinitely...\n";<br>
sleep(5);<br>
}<br>
?><br>

This script will not be forcibly terminated due to execution time limits.


4. Example of Extending Execution Time for curl Requests

When accessing remote APIs, network delays may cause timeouts. We can extend script execution time to ensure the request completes.

<?php
// Extend the maximum script execution time
set_time_limit(120);
<p>$url = "<a rel="noopener" target="_new" class="" href="https://gitbox.net/api/data">https://gitbox.net/api/data</a>";<br>
$ch = curl_init();</p>
<p>curl_setopt($ch, CURLOPT_URL, $url);<br>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);</p>
<p>$response = curl_exec($ch);<br>
if (curl_errno($ch)) {<br>
echo 'Curl error: ' . curl_error($ch);<br>
} else {<br>
echo "Response: " . $response;<br>
}</p>
<p>curl_close($ch);<br>
?><br>

Here, https://gitbox.net/api/data is an example request URL, and set_time_limit(120) ensures the script has enough time to wait for the response.


5. Summary

  • set_time_limit() is an important function to control the maximum execution time of PHP scripts.

  • Using this function properly helps prevent scripts from timing out and interrupting tasks, especially for time-consuming operations.

  • Be mindful not to set the execution time to unlimited indiscriminately, to avoid wasting server resources.

  • Setting execution time appropriately is best practice when handling time-intensive tasks like network requests and file processing.

Mastering the use of set_time_limit() can make PHP scripts more stable and efficient, reducing the annoyance of timeout errors.