# How to Increase max_execution_time in PHP

In this quick article, we’ll explore the different options that allow you to increase the maximum amount of time a script is allowed to run before it’s terminated by the parser in PHP.

## What Is the max_execution_time Directive in PHP?

The max_execution_time directive sets the maximum amount of time a script is allowed to run before it is terminated. The default is 30 seconds, and you can increase it to a reasonable limit as per your requirements.

More often than not, you will have noticed that if a script takes too long to execute, PHP throws the famous maximum execution time exceeded error:

In most cases, the max_execution_time directive is not customized, and thus it defaults to 30 seconds, and you could end up with this error.

In the next section, we’ll discuss a couple of different ways to increase the value of the max_execution_time directive.

## How to Increase max_execution_time in PHP

There are a few different ways to increase the script execution time.

### The ini_set Function

This is one of the easiest ways to increase the value of the max_execution_time directive. The ini_set function is used to change the value of the configuration directives that are available in the php.ini configuration file. And thus, we can use it to change the value of the max_execution_time directive.

To increase the script execution time, you can use the following snippet at the top of your script.

In the above example, it would set the max_execution_time directive to 300 seconds.

On the other hand, if you set it to 0, it would allow the script to run for an infinite amount of time. You must be very careful when you exercise this option, and it’s never recommended in production.

### The php.ini Configuration File

If you want to increase the value of the max_execution_time directive globally, you should use this option. Of course, you need permission to change the php.ini file.

Go ahead and locate the php.ini file on your server. If you don’t know how to do it, I explain how to find the php.ini file in another article.

Once you’ve located the php.ini file, open it with your favorite text editor and look for the following line:

Change it as per your needs, save it, and restart your web server.

### The set_time_limit Function

Apart from the ini_set function which we’ve just discussed, there’s also another PHP function which allows you to set the maximum execution time of a script. The set_time_limit function allows you to set the number of seconds a script is allowed to run.

Let’s quickly look at how the set_time_limit function works.

As you can see, the set_time_limit function takes a single argument: the number of seconds. If you set it to 0, a script is allowed to run for an infinite amount of time.

In fact, the set_time_limit function works a bit differently from the max_execution_time directive. When the set_time_limit function is called, it resets the timeout counter to zero. And from there onward, it measures the script execution time which you’ve set with the set_time_limit function.

Let’s assume that a script is executed for 10 seconds, and then the set_time_limit function is called to set the script execution time to 30 seconds. In that case, a script would run for a total of 40 seconds before it’s terminated. So if you want to use the set_time_limit function to set the maximum execution time of a script, you should place it at the top of your script.

### Apache Users: The .htaccess File

If you’re using the Apache server to power your website, it provides the .htaccess configuration file, which allows you to set php.ini configuration directives. In this section, we’ll see how you can use the .htaccess file to set the max_execution_time directive.

First of all, you need to locate the .htaccess file in your project. In most cases, you should be able to find it in the document root of your project. If you can’t find it in the project root, you need to make sure that it’s not hidden by your file manager software, since the dot before the .htaccess file name indicates that it is a hidden file. You can use the show hidden files option in your file manager to see all the hidden files in your document root. If you still can’t find it, you need to create one.

Open the .htaccess file in your favorite text editor and add the following line at the end.

Save it, and you’re done!

## Conclusion

Today, we discussed a few different ways to change the value of the max_execution_time directive in PHP.

### Learn PHP With a Free Online Course

If you want to learn PHP, check out our free online course on PHP fundamentals!

In this course, you'll learn the fundamentals of PHP programming. You'll start with the basics, learning how PHP works and writing simple PHP loops and functions. Then you'll build up to coding classes for simple object-oriented programming (OOP). Along the way, you'll learn all the most important skills for writing apps for the web: you'll get a chance to practice responding to GET and POST requests, parsing JSON, authenticating users, and using a MySQL database.