If you run file_get_contents() in PHP it reads the file in as plain text. If your file contains PHP, that PHP won’t execute and you will just read in the raw code. You can get around that by using the output buffer.
Output buffering in PHP stores whatever you echo into a buffer so you have control over when it is output. This code will show you how to use it:
// Start the output buffering
ob_start();
// Include your file; instead of being output immediately it will go into the buffer
include 'test.php';
// Get the output from the buffer and store it for later
$output = ob_get_clean();
// Output your code; now you have something that works like file_get_contents() but it executes PHP
echo $output;
// You can also defer the output for later; it’s not just a file_get_contents() replacement
Tim Bennett is a Leeds-based web designer from Yorkshire. He has a First Class Honours degree in Computing from
Leeds Metropolitan University and currently runs his own one-man web design company, Texelate.