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:
max_execution_time = SECONDSin php.ini.
- On an Apache webserver, add
php_value max_execution_time SECONDSin the
set_time_limit(SECONDS)in the PHP script.
- Finally, we can set the time limit using
That’s all for the basic, read on if you need detailed examples!
REAL QUICK SLIDES
TABLE OF CONTENTS
|PHP Time Limit||Useful Bits & Links||The End|
WAYS TO SET THE TIME LIMIT
All right, let us now get into the examples of setting the execution time limit in PHP.
; 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.
2) APACHE WEB SERVER – 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.
3) INLINE SET TIME LIMIT
<?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.
4) ALTERNATIVE TIME LIMIT SET
<?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.
USEFUL BITS & LINKS
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:
; Maximum amount of memory a script may consume (128MB) ; http://php.net/memory-limit memory_limit=128M
php_value memory_limit 256M
INFOGRAPHIC CHEAT SHEET
LINKS & REFERENCES
- Set Time Limit – PHP
- ini_set – PHP
- How to change configurations settings – PHP
- PHP Background Processes – Code Boxx
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!