How to Run Large or Long Running PHP Scripts

Many times, problems arise while trying to run PHP scripts that take to long to run, take up to much memory or both. There is always a solution of course. If you are lucky, your solution will be one of the easy ones given here. Otherwise, you are in for a hard time and a little more work than you prefer, I’m sure.

If you are lucky, these two magic lines of PHP code added to the top of your PHP script will work:

//increase max execution time of this script to 150 min:
ini_set(‘max_execution_time’, 9000);
//increase Allowed Memory Size of this script:
ini_set(‘memory_limit’,’960M’);

…if not, you are pretty much screwed. Sorry. But here are some possible solutions.

Change the php.ini settings to allow more execution time and memory

This method is as simple as locating your server’s php.ini file and finding all settings that mention execution time and memory limit and increasing the numbers or setting them to -1(unlimited). The problem with this is that many of you are probably on a Godaddy, Hostgator or similar shared server that doesn’t give you access to these settings. If you are lucky enough to have a dedicated server that gives access to php.ini, then this may be the ultimate solution for you however and it also eliminates the need to put the above mentioned PHP code in the top of all of your PHP scripts.

Change the way your PHP Code Functions

In theory this sounds easy enough. Not always in practice however. It has saved my ass before when none of the above methods would work. Usually what makes a PHP script run too long or use too much memory is some type of PHP loop. To make that long-running script run without running out of memory or time, you have to periodically cause the script to write the output to the page to prolong execution time.

Another example of changing your code is to make changes that allow your code to run faster and hopefully fall short of the max execution time. One such method for speeding up PHP loops is to use ob_start() at the beginning of your code. ob_start() gives you a 5-15% boost in speed with little effort on your part.

Update: I actually did a benchmark test on the above ob_start() method after writing this. I was pleasantly surprised to discover that it increased my script’s speed by the max suggested, 15%. I had a loop that did a lot of different things with domain names and without ob_start() at the start of the script it always returned right around 30 results. After adding ob_start() and nothing else to that script it was getting 45 results instead of 30 which is on the top end of what I read was to be expected.

Fix PHP Loops that Timeout Before Finishing

To fix a for or while loop that won’t complete before it times out, try this secret I discovered.

Add this code right after starting the loop:

ob_start();

…and this code right before ending the loop:

echo ob_get_contents();
ob_end_flush();

Example:

foreach($xlsx->rows() as $k => $r) {
ob_start();
//whatever long running code here….

echo ob_get_contents();
ob_end_flush();
}//end foreach loop.

That is what saved my ass when I couldn’t figure out how to get a for loop to work that kept timing out after 20 seconds while building a WordPress plugin with PHP.

I don’t have time to put all the answers for optimizing PHP code for speed and memory here, but here is a link that provides much more information:

http://phplens.com/lens/php-book/optimizing-debugging-php.php

…and learn more about ob_start() here:

http://php.net/manual/en/function.ob-start.php

 

 

One Reply to “How to Run Large or Long Running PHP Scripts”

Leave a Reply

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