X

PHP max_execution_time Guide: What is the default limit, and how can it be increased?

Spread the love

This tutorial will explain the PHP value max_execution_time and its use.

What is PHP max_execution_time?

The PHP max_execution_time is a PHP value that sets the maximum time in seconds a script is allowed to run before the system terminates it. The default limit is 30 seconds. Since every installation of PHP usually has predefined values, these values are not appropriate for most websites, which need different values.

On non-windows systems, the maximum execution time is unaffected by system calls, stream operations, etc. Sometimes, the web server has other limitations that can interrupt the PHP execution. Apache has a timeout directive set to 300 seconds. Every web server has its specific details.

How do we increase the PHP max_execution_time?

If you are facing issues related to PHP script timeout, then most probably, the max execution time is using the default value of 30 seconds, and that is not enough for the script to be executed. The max execution time hits the limit when the script cannot process the data that fast. There are different ways to change the value for max execution time, depending on where we want to change it, for example, in the code directly, in the .htaccess file, or the php.ini file.

Change PHP max_execution time in the code: To change the code directly, we must use the function set_time_limit (n) or ini_set(‘max_execution_time’). Here is one example of how to do it:

<?php

set_time_limit(40);

while ($i<=10)
{
        echo "i=$i ";
        sleep(100);
        $i++;
}

?>

In this example, we set the max execution time to 20, allowing the while loop to execute successfully. The other way is with the ini_set(‘max_execution_time’,n)

<?php

ini_set('max_execution_time',40);

while ($i<=10)
{
        echo "i=$i ";
        sleep(100);
        $i++;
}

?>

In both cases, the functions for max execution time will not count the time cost of sleep.

Increase PHP max execution time in .htaccess: To increase the PHP max execution time in the .htaccess file, you need to paste the following lines of code:

php_value max_execution_time 100

Increase max execution time in php.ini: To increase the PHP max execution time in the php.ini file you need to use the following line of code:

max_execution_time = 100

The previous paragraph taught us to increase the PHP max_execution_time directly in the code, .htacces, and php.ini files. Nowadays, there are control panels on the server to simplify the work of system admins. Every control panel has its way of increasing it, but the purpose is the same. The control panels in the background use the same functions we mentioned above.

Conclusion

That’s it. You learned what PHP max execution time is, what the default limit is, and how to increase it. If you plan to host a PHP application and have an issue with timeout about the scripts, you do not have to do this alone. You can sign up to one of our server management plans and submit a support ticket. Our admins will help you with any aspect of the PHP values. Changing them according to the needs of your website.

PS. If you liked this post, please share it with your friends or leave a comment below. Thank you.

Categories: Guides
admin:
Related Post