Archive for May 2nd, 2008

02
May
08

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:

1. put 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

2. put 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.

3. put 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:
ini_set('output_buffering', 0);
ini_set('implicit_flush', 1);
ob_end_flush();
ob_start();

then after each write (echo, print_r, etc) put ob_flush(); flush();
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)




c

Blog Stats

  • 62,529 hits

 

May 2008
S M T W T F S
« Apr   Jul »
 123
45678910
11121314151617
18192021222324
25262728293031