4 Ways To Set Execution Time Limit in PHP (Quick Examples)

Welcome to a tutorial on how to set the execution time limit in PHP. So you want to change the allowed execution time in PHP to prevent timeout errors on a massive script?

There are 4 ways to change the execution time limit in PHP:

  1. Change max_execution_time = SECONDS in php.ini.
  2. On an Apache webserver, add php_value max_execution_time SECONDS in the .htaccess file.
  3. Use set_time_limit(SECONDS) in the PHP script.
  4. Finally, we can set the time limit using ini_set("max_execution_time", SECONDS).

That’s all for the basic, read on if you need detailed examples!

 

 

TABLE OF CONTENTS

 

WAYS TO SET THE TIME LIMIT

All right, let us now get into the examples of setting the execution time limit in PHP.

 

METHOD 1) PHP.INI

php.ini
; Maximum execution time of each script, in seconds
; http://php.net/max-execution-time
max_execution_time=180

If you have access to the php.ini file on the server, the easy way is to just change the max_execution_time directive. But please take note that it will be applied to all PHP scripts, I will not recommend doing so unless you intend to do some long processing on the entire server.

 

METHOD 2) APACHE WEB SERVER – HTACCESS

.htaccess
php_value max_execution_time 180

If you are running an Apache web server, the time limit can also be changed by creating a .htaccess file in the project folder. But take note again, this will apply to the entire project folder.

 

 

METHOD 3) INLINE SET TIME LIMIT

time-limit.php
<?php
set_time_limit(180); // Time limit in seconds

This is what I will normally recommend – Change the time limit using the set_time_limit() function in the script itself. This way, it will only apply to this script and not disturb all the other parts unnecessarily.

 

METHOD 4) ALTERNATIVE TIME LIMIT SET

time-limit.php
<?php
ini_set("max_execution_time", 180);

This final method should be pretty self-explanatory, the ini_set() function allows us to change some of the settings in php.ini during run time. So, Captain Obvious, this example is changing max_execution_time for the duration of the script only.

 

 

EXTRAS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

EXTRA) COMMAND-LINE SCRIPTS

Please take note that if you run a PHP script in the command line (or terminal), the time limit will automatically be set to 0. That is, there will not be any time limits.

 

EXTRA) SETTING THE MEMORY LIMIT

Apart from the time limit, you may also run into a memory limit problem when crunching large datasets. Thankfully, this soft limit can also be lifted in a similar manner:

php.ini
; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit=128M
.htaccess
php_value memory_limit 256M
set-memory.php
ini_set("memory_limit", "16M");

 

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!