making bbPress (and WordPress) work better!

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);

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)

8 responses

  1. nice can i support this blog?

    June 25, 2008 at 3:00 am

  2. mmmmmmmmmm…no answer for this??!!

    September 25, 2008 at 5:53 pm

  3. i’m trying trick number 4, but it doesn’t seem to work😦 it just dumps the output at the end all at once – i can’t figure it out – it’s on a VPS – i think im still on apache 1.3 – can i have it temporarily disable mod_gzip for that file?

    July 30, 2009 at 9:30 pm

  4. Daniel if your server is using real mod_gzip, that will definitely prevent output buffering from working properly.

    However that also means you could control it via .htaccess

    You could use conditionals to turn off mod_gzip in .htaccess for a specific filename or directory.

    What you want to set is
    mod_gzip_on No

    The problem is I am not certain how to make the if statement, I think it will have to be via SetEnvIf

    You might be able to ask this on
    ie. “How can I turn off mod_gzip for just one file via .htaccess”

    July 30, 2009 at 11:43 pm

  5. Well I got it working now – mod_gzip was off before, but somehow it’s working now. I did so many things that I don’t know what did it – first I dumped 4k of nothing to clean out the buffer, but i think i did that before when it wasnt working. I also upgraded to apache 2 and php 5 – i think i read somewhere that in apache 1 you couldn’t turn off buffering for some reason.

    October 16, 2009 at 11:25 am

  6. Worked like a charm!

    The only thing I had to edit was replacing ob_end_flush(); with try { while( @ob_end_flush() ); } catch( Exception $e ) {} to avoid an error.

    August 13, 2011 at 5:39 pm

  7. Anonymous

    thanks, works perfect. 2-3 methods are too small for my script, but last works perfect.

    October 14, 2013 at 4:06 am

  8. Anonymous

    Very nice – thank you.

    October 30, 2015 at 12:16 pm

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s