Open In App

How to increase execution time of a PHP script ?

Last Updated : 20 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to discuss how to increase the execution time of a PHP script.

During executing a PHP script we may come across to fatal error due to maximum time exceeded. The default maximum execution time of a PHP script is 30 seconds. So if the running of the script exceeds 30 seconds then the system will throw an error. There may be cases such that script running time takes beyond 30 seconds. In those scenarios, we are supposed to increase the execution time. This can be done using the ini_set() function.

ini_set(): The ini_set() function is used to modify the settings in the php.ini file. It accepts two parameters, one is the name of the setting to be modified and the other is the value to be assigned. To increase the execution time, we use a string called max_execution_time which is a setting name in the php.ini file. 

Syntax:

ini_set('setting_name', value);

Parameters:

  • setting_name: It specifies the name of the setting we need to modify.
  • value: Assigns the value to the setting.

Note: This ini_set() function must be specified in the first line of the script.

Let us look into a few examples that use the ini_set() function to increase execution time.

Example 1:The below code does not even take one second to execute but we increased the maximum execution time limit to 60 seconds.

PHP




<?php
    ini_set('max_execution_time', 60);
  
    // Increase execution time limit to 60 seconds
    echo "GFG Learning portal"
?>


Output:

GFG Learning portal

Example 2: We can also set the maximum execution time to unlimited by providing 0 as a value to the ini_set() function. But it is not an ideal approach to follow. Let us look into code on how to set the maximum execution time to “unlimited”.

PHP




<?php
    ini_set('max_execution_time', 0);
  
    // No maximum execution time (unlimited)
    echo "GFG Learning portal"
?>


Output:

GFG Learning portal

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads