Tricks for long or large PHP scripts
I have written some extensive scripts for PHP to do things it probably was not meant to do.
Unfortunately some get so large and take so long that I’ve researched a few tricks that may be helpful to others:
error_reporting(E_ALL); at the start to make sure you find out any little errors like undeclared strings or unexpected output – very helpful for script that may take long periods of time to execute and you want to get it right the first time
ini_set("max_execution_time", "300"); at the start to extend the timeout (typically 30-60 seconds in a default PHP setup). If you are on a shared server this ability may be locked out to you. 300 is an example for 5 minutes.
ini_set('memory_limit','64M'); at the start to boost your memory limit for very complex arrays, etc. The default is typically 16M. Again, this may be unavailable to you on a shared server.
4. best trick of all – unbuffered output to browsers in HTML, so you can see results in realtime, even if the script takes 5+ minutes, etc.
put at the start:
then after each write (echo, print_r, etc) put
You’ll know it’s working when you see each line appear as it happens in your browser window.
Note if you have an older Apache 1.x server that uses mod_gzip this trick may not work. It should however on Apache 2.x, lighttpd, litespeed (maybe even IIS, but I dunno)