How increase executation time of a php script

02nd, January 2018 By

Some time we have very big script or query executed in very big database ,so need much time to execute them but the page load fails because of less time set for the execution of the script.

By default the execution time for running a php script is 30 seconds,which have been set in our php.ini file.

max_execution_time = 30;   //In your php.ini file

The max_execution_time function sets the maximum time in seconds a script is allowed to run before it is terminated by the parser.So the badly written scrips or query do not down the server.You can modify this function to change the execution time of script running.

Use set_time_limit() Function

You can increase the script execution time from your script file using set_time_limit() function.Just add this function at the top of your script php file.

 set_time_limit($seconds);

$sceond = The maximum execution time, in seconds. If set to zero, no time limit is imposed.

Where $second is the number of second the execution time of the script.You can change it by adding any time in second for running script

set_time_limit(60);

But this function not working if your safe mode is on ,in your server.Its works when you turn off the safe mode.Now for example using this function and set time limit to 60 seconds ,the script will run up to time  60 seconds if more time take page will not load and send any error.Its not return any value.

Use ini_set() Function

You can also increase the execution time of script running by using ini_set ()  function.

ini_set('max_execution_time', 200);

Where 200 = time in second

Just add given function at the top of the your php file.

You can change it by adding the time which you like to use for run the script.This function also not working when Safe Mode is on ,in your server.Foe make it working off the Safe Mode.ini_set() returns FALSE on failure and does not trigger warnings.

Using Sleep() Function

The sleep() function is used to delay the execution time of the script.

sleep($seconds);

$seconds = Time in Seconds

It will return zero on success and error on false.

 <?php

echo  "How are you dear \n";

// sleep for 10 seconds
  sleep(10);

 // wake up !

echo "Welcome to websexpert \n";

 ?>

This example will echo the “How are you dear” then after 10 seconds echo the “welcome to websexpert”.So delay of 10 seconds for the execution of the script.

You can also use usleep() function for delay execution in microseconds,time_nanosleep() function for delay for a number of seconds and nanoseconds,time_sleep_until() function for make the script sleep until the specified time.

Leave a Reply

Your email address will not be published. Required fields are marked *