We’ve all come across the dreaded fatal error of max execution time once or twice, it can be a pain. PHP’s maximum execution time is set at 30 seconds as a bare default, but in some cases, it needs to be higher. PHP has your back though, you can change the default settings by using one function; ini_set(). It takes two parameters, a string, and an integer. The first is the exact setting you want to change, and the second is the number of seconds you want to set that setting at.
Increase max execution time for a single PHP script
To quickly fix your issues for heavy scripting, add the following line of PHP to the very top of your script.
Two important points to note –
- The setting change must be done at the very beginning of the script, otherwise, you risk executing code against the default and the newly set time, which isn’t going to turn out well.
- This will only apply to the page you add the line of code too.
ini_set('max_execution_time', 120); // 120 (seconds) = 2 Minutes
Setting an unlimited maximum executing period
If you’re not happy with specifying an exact number of seconds, you can have it set to unlimited, meaning there is no maximum time of execution. This, of course, is kinda bad practice, but if it’s for development and testing purposes, it’s pretty handy. Just make sure you revoke the setting before you push your changes live again.
ini_set('max_execution_time', 0); // 0 = Unlimited
Set Max_Execution_Time globally in php.ini
You can issue this setting to all of your projects pages or scripts by using the global PHP.ini file. By changing it’s a default setting to whatever you require, then it is possible that you won’t really need to set it in the script anymore. To change your execution time in php.ini follow the steps below
- Locate and open your PHP build folder
- Find the php.ini file and open it
- Find the following line in the text configuration file – max_execution_time=30
- Change the value 30 to the value of choice, Remember, this value is in seconds.
- Save & Close.
- Restart your web server for changes to take effect.
Take a look at the documentation of both in-built PHP-functions that were talked about in this article –
Changing this setting shouldn’t be done so impetuously, think of the drawbacks first, especially if it’s going to be on a live project. This little setting can be a lifesaver, but if used incorrectly, is a ticking-time-bomb for your web-server.