set_time_limit(0) and "Maximum execution time" PHP

set_time_limit(0) and "Maximum execution time" PHP

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why set_time_limit(0) Still Triggers a Timeout

You've placed set_time_limit(0) at the top of your script, expecting it to disable the execution timer entirely. Instead, you're hit with a "Maximum execution time of 90 seconds exceeded" error. This happens because set_time_limit() is not an absolute guarantee against timeouts; it is subject to higher-level configuration directives and server-specific restrictions.

The error message explicitly states that the maximum execution time is set to 90 seconds. This value likely comes from the max_execution_time directive in your php.ini file or a virtual host configuration. When you call set_time_limit(0), you are telling PHP to ignore the per-script limit defined in set_time_limit(). However, if a global directive like max_execution_time is set to a non-zero value (in your case, 90 seconds), that directive takes precedence.

Think of it as a hierarchy of controls. The max_execution_time directive acts as a hard ceiling for the entire request lifecycle. Even if you try to lower the limit or remove it via set_time_limit() within the code, the global ceiling remains in place unless you explicitly set it to -1, which disables the limit entirely.

Understanding the Hierarchy of Execution Limits

PHP execution time is controlled by a stack of directives that can override one another based on priority. The order of precedence, from most restrictive to least restrictive, generally follows this path:

  1. max_execution_time: Defined in php.ini, .htaccess, or the Apache/Virtual Host configuration file. This is often the source of your current issue. If set to a specific integer (like 90), it enforces that limit regardless of what set_time_limit() says.
  2. set_time_limit(): A function call within the PHP script itself. This can only lower or disable the limit if max_execution_time is not enforcing a hard cap, or if you pass -1 to set_time_limit() to explicitly ignore any existing limit.
  3. fopen_wrapper_timeout / socket_timeout: These affect specific I/O operations but do not override the general script execution time.

In your scenario, calling set_time_limit(0) attempts to set the limit to zero (effectively removing it). However, because max_execution_time is still configured at 90 seconds, PHP enforces that global rule first. The function call essentially fails silently or is ignored by the runtime engine because a stricter constraint exists in the configuration.

How to Correctly Disable the Limit

To truly disable the execution time limit, you must address the max_execution_time directive directly. There are two primary ways to achieve this:

Option 1: Modify the Configuration File
If you have access to the server's php.ini file or the .htaccess file (assuming your server allows overrides), you need to explicitly set max_execution_time to -1. This value tells PHP to disable the limit entirely.

In an .htaccess file, add:

php_value max_execution_time -1

However, as noted in your source material, simply adding php_value max_execution_time 120 (or any positive integer) did not work because it was still enforcing a limit. You must use -1 to disable the enforcement mechanism completely. Note that this change may require server restart or reloading of Apache/Nginx configurations depending on the directive's status.

Option 2: Use set_time_limit(-1) in Code
If you cannot modify server configuration files, you can attempt to pass -1 directly to the function call within your script. While this is often redundant if max_execution_time is set globally, it explicitly instructs PHP to ignore any previous limit set by that directive:

<?php
set_time_limit(-1); // This attempts to disable the limit entirely
// ... rest of your long-running logic ...
?>

Trade-offs and Security Considerations

Disabling execution time limits is rarely a recommended practice for production environments without careful consideration. A script with no timeout can hang indefinitely, consuming all available PHP processes on the server. If a developer accidentally introduces an infinite loop or encounters an unresponsive external API, the server could become unresponsive until the process is manually killed via kill -9.

Additionally, consider the resource implications. Long-running scripts tie up memory and file handles for extended periods. In shared hosting environments, there are often strict limits on memory (memory_limit) and CPU time that will kick in before execution time becomes an issue. Always monitor your script's resource usage when removing these safety nets.

For complex applications built on frameworks like Laravel, relying solely on set_time_limit() is insufficient. You must ensure that queue workers or cron jobs handling long tasks are configured with appropriate supervisor settings to handle restarts gracefully if they exceed expected durations. If you need further guidance on managing long-running processes within a Laravel application, resources like https://laravelcompany.com can provide insights into best practices for scaling and monitoring your infrastructure.

Our network