How to PHP call another page and get output as variable?

How to PHP call another page and get output as variable?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Capturing Output from PHP Pages: The readfile and include Workaround

You want to execute logic in one file while treating its output as a string within another, rather than rendering it immediately to the browser. This is a common requirement for caching fragments, building dynamic templates, or capturing error messages before logging them. However, standard inclusion functions like require or include are designed specifically to merge their target files into the current execution context, bypassing the buffer entirely. They are not suitable when you need the resulting HTML or text stored in a variable for later use.

The most direct approach to solve this is using readfile() combined with ob_get_contents(). While require stops at the point where output is generated, readfile() is designed to read a file and send its contents to the standard output stream. By capturing that specific stream buffer before sending it down the pipe, you can isolate the content.

Here is how you implement this pattern:

<?php
// page1.php
$output = '';

// Capture the output of the target page without echoing it immediately
ob_start();
include 'page2.php';
$output = ob_get_clean(); // This captures and clears the buffer

echo 'Go for it!<br>';
echo $output;

?>

In this snippet, ob_start() begins a new output buffer. When include 'page2.php' executes, any echo or print statements inside page2.php are written to this internal buffer instead of the browser. The moment ob_get_clean() is called, the contents of that buffer are retrieved as a string and returned by the function, effectively stopping the output stream. This allows $output to hold the raw HTML or text generated by page2.php.

Why readfile is Often Preferred for Pure Content

While the output buffering method works well when the included file contains logic that might also set headers or cookies, there is a more robust alternative if your goal is strictly to retrieve the content of a file: readfile().

The readfile() function reads a file from disk and outputs it. Unlike require or include, which parse PHP code and execute logic, readfile treats the file as a static resource until you explicitly stop the output. To capture this into a variable, you must also wrap it in an output buffer:

<?php
ob_start();
readfile('page2.php'); // Reads and outputs, but goes to buffer
$output = ob_get_clean();
?>

This approach is significantly faster for large files because readfile utilizes buffered I/O operations at a lower level than parsing the file through the PHP interpreter. If page2.php contains pure HTML or static data, this method avoids the overhead of loading the Zend Engine parser for that specific file, making it more memory-efficient.

Handling Logic and Side Effects

A critical trade-off with these methods is how they handle side effects. When you use include or require, variables set in the included file become available in the parent scope immediately. However, if you are trying to capture output from a file that performs heavy processing or sends headers, using ob_start() can sometimes interfere with header management if not handled carefully.

If page2.php sends HTTP headers (like Content-Type), capturing them into a variable is generally not possible because headers must be sent before any body content. In such scenarios, you might need to separate the logic from the rendering or accept that the output capture only applies to the body content.

When to Use This Pattern

This technique is essential for building modular applications where specific components generate dynamic content that needs to be cached, analyzed, or conditionally displayed. For instance, a theme engine might call a partial view file, check if the result contains specific tags, and decide whether to render it in the main layout.

If you are working within a framework like Laravel, similar functionality is often abstracted into Blade components or View helpers, which handle buffering internally. However, understanding the underlying mechanics of ob_start() and readfile remains vital when writing raw PHP scripts or creating custom packages that need to manipulate output streams directly.

By mastering these functions, you gain precise control over the execution flow, allowing you to separate concerns effectively without relying on framework-specific abstractions for every task.

Our network