Blog Search

Latest Posts

We are now trading as a Limited company

Recent Work: Digital Marketing Material and Website Re-Designs

We are now VAT Registered

Affordable Web Hosting and Domain Name Registration

New Acuras Logo Coming Soon...

Barclaycard ePDQ CPI Integrations

HSBC XML API / CPI E-commerce Integrations

Photoshop to Valid XHTML/CSS Coding Service

Recommended Open Source Invoicing System

Reasons Businesses should Embrace the Web

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 Bookmark and Share

Related Links

Related Articles / Posts

Barclaycard ePDQ CPI Integrations (27/05/2009)

HSBC XML API / CPI E-commerce Integrations (02/04/2009)

Recommended Open Source Invoicing System (25/03/2009)

Recent Recommended Links - October 2008 (03/10/2008)

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

 
Acuras Web Development Paje House 164 West Wycombe Road High Wycombe HP12 3AE United Kingdom 08712 887 565
Tel: 08712 887 565
Fax: 08712 887 566
Email: info@acuras.co.uk
Skype: acuras_web
Paje House
164 West Wycombe Road
High Wycombe
Buckinghamshire
HP12 3AE