How to Extend the Run Time of a PHP Script

PHP



In this article, we show how to extend the run time of a PHP script.

The run time of a PHP script is the amount of time that it takes for a PHP script to execute.

The PHP setting that correlates with the maximum run time for a PHP script is the max_execution_time setting.

The max_execution_time setting is the maximum amount of run time that a PHP script can have before the script gets timed out by the server and fails to be executed.

By default, on most PHP server settings, the max_execution_time is set to 30 (meaning 30 seconds).

PHP is a fast-performance language, so for practically all basic scripts and most intermediate scripts, 30 seconds is more than sufficient for the amount of time it takes a server to process and execute a PHP file.

However, for certain applications, such as those that are extremely data-intensive or memory-intensive, 30 seconds may not be enough.

One type of script this may be the case for is a spider crawler for a relatively large site. If a spider is crawling a site to gather hyperlinks and there are thousands of files that it has to go through, this can take a substantial long time, longer than 30 seconds. If it takes longer than 30 seconds, with the default PHP max_execution_time setting, the script will clock out and fail to execute and crawl the sites.

So in a case like this, you need to extend the run time of the PHP script.

So there are 2 ways of doing this.

One, we can manually change the setting in the php.ini file. If you do this, it applies systematically to the whole server. So it affects all pages on the your website.

Second, we can change the setting in the currently running PHP script. When we do this, it applies only to the script, meaning this setting is only changed for the script that has it. All other pages on the site are not affected.

To change the max_execution_time setting in a PHP script, we use the PHP ini_set() function.

The ini_set() function accepts 2 parameters.

The first parameter is the setting you want to change which is the max_execution_time setting.

The second parameter is the value that you want to set the max_execution_time setting to.

The following code converts the maximum run time to 200 seconds.



So with this code above, we've now extending the maximum run time over 6 times its default value. A PHP script can now be processed over the server for up to 200 seconds instead of just 30 seconds. If you want, you can even extend this value even more, depending on how complex your script it.

And this is a quick showing how the run time of a PHP script can be extended.


HTML Comment Box is loading comments...