Blog Search

Latest Posts

Firefox 3 Web Browser Launched

New Educational Media Website Launch

Trevor Laurency Fitness Website Launch

Recent Recommended Links - April 2008

Web Apps on Your Desktop with Mozilla Prism

Recent Recommended Links - March 2008

Ubuntu Linux - Persistent sudo root

Controlling PHP Register Globals Using .htaccess File

Javascript Essentials - Date Pickers (Calendars)

The new website is here!

Blog Archive

Complete Blog Archive

Browse by Subject

AJAX
Apache
CMS
CSS
HTML
Java
JavaScript
Links
Linux
Mac
MySQL
Pascal
PHP
postgreSQL
RSS
Ruby
SEO
Web Dev News
Windows
XML

Other Resources

Matt's Web Development Blog

Computer Science Student Articles

Affiliate Program Articles

PHP: Include a file and store contents to variable using output buffer

Bit of an obscure post this but I came across this very handy feature of PHP which allows you to include a file but store the contents of the included file to a variable. This is particularly useful for PHP scripts and software which output content to a page by using variables. Many CMSs are built like this.

The principal behind this solution is to use an output buffer. You start the output buffer, include the file(s), store the included content to a variable, then stop the output buffer. Code is shown below:

ob_start(); // start buffer
include ("file-to-include.php");
$content = ob_get_contents(); // assign buffer contents to variable
ob_end_clean(); // end buffer and remove buffer contents
echo $content;


Hope this comes in useful. Bye for now!

Subscribe to RSS Feed Digg! Add to Google Add to My Yahoo!

Related Links

Related Articles / Posts

Controlling PHP Register Globals Using .htaccess File (04/03/2008)

PHP: Include a file and store contents to variable using output buffer (05/06/2007)

PHP Mailer Class - Sending SMTP Email (22/03/2007)

Code Editors (Syntax Highlighting) - Aptana, Crimson Editor, PHP Designer (14/08/2006)

Web Dev News: Pro Ajax and PHP - Building Highly Interactive Applications (Book) (18/07/2006)

 
Acuras Web Development 21 Hylton Road Sands High Wycombe HP124BZ United Kingdom 08712 887 565
Tel: 08712 887 565
Fax: 08712 887 566
Email: info@acuras.co.uk
Skype: acuras_web
21 Hylton Road
High Wycombe
Buckinghamshire
HP12 4BZ
United Kingdom
Feeds / Content
News
Latest Links
Blog - PHP / CSS