AndrewDavidson.com.  We don't reinvent the wheel... we just make reinventing the wheel easier. Visit PDFCalendar.com for free customizable printable calendars.
Home
Resources
Software
Links
Articles
About
About Drew
Contact
Feedback
Subscribe to our Newsletter:
Printer-Friendly
© 1999-2024
Andrew Davidson.
All Rights Reserved.

Do you call flush() after every echo or print in PHP?

It's not necessary. You can simply enable auto-flush at the start of your script and be done with it.
Copyright © 2005 Andrew B. Davidson. All Rights Reserved.
Last Updated: December 2005

ob_implicit_flush is your friend.

If you call the PHP function ob_implicit_flush at the start of your PHP script, PHP will automatically call flush immediately after every print or echo.
However if you use mod_gzip or other output buffering (e.g. with ob_start etc) then this is may not be for you! Because, ob_implicit_flush will disable output buffering. It really means "I want no buffering".
Note that calling ob_implicit_flush() with no parameters is the same as calling ob_implicit_flush(TRUE). TRUE means turns on auto-flush.
Read the ob_implicit_flush documentation for full info.

Read the documentation?! Just tell me how to use it!

Ok, sheesh. Put this at the start of your script and then you wou't have to call flush() every time you call print or echo:
// TRUE = disable all output buffering,
// and automatically flush()
// immediately after every print or echo
ob_implicit_flush(TRUE);

How do I know if it's for me?

If calling flush() does what you want, then this will do what you want. It just makes it so that you don't need to call flush anymore. If you find yourself calling flush a lot, try this.

How do I know if it's NOT for me?

It's not for you if you use output buffering on purpose, because ob_implicit_flush disables all output buffering.

But isn't ob_implicit_flush just about ob_* calls? Does it really apply to flush, echo, and print calls?

ob_implicit_flush is about all output: flush, echo, and print included. Don't let the ob_ distract you! It's not just about ob_* calls like ob_start and ob_flush, and ob_end_flush.
The documentation states: "explicit calls to flush() will no longer be needed" if you call it.
I hope this information helps your PHP web development efforts!
 
In association with
Amazon.com