PHP Output Control

Manage and buffer PHP output

๐Ÿ“ค What is Output Control?

Output Control functions let you manage when and how PHP sends output to the browser. You can buffer output, modify it before sending, or control headers and redirects.


<?php
ob_start(); // Start buffering
echo "This is buffered!";
$content = ob_get_clean(); // Get and clear buffer
?>
                                    

Key Output Control Functions

โ–ถ๏ธ

Start Buffering

Begin output buffering

ob_start();
๐Ÿ“ฅ

Get Buffer

Retrieve buffered content

$content = ob_get_contents();
๐Ÿงน

Clean Buffer

Clear buffered output

ob_clean();
๐Ÿ“ค

Flush Buffer

Send buffer to browser

ob_flush();

๐Ÿ”น Basic Output Buffering

Output buffering stores PHP output in memory instead of sending it immediately to the browser. This gives you control to modify or discard output before it's sent.

<?php
// Start output buffering
ob_start();

echo "Hello ";
echo "World!";

// Get buffer contents
$output = ob_get_contents();

// End buffering and send output
ob_end_flush();

echo "<br>Buffer contained: " . $output;
?>

Output:

Hello World!
Buffer contained: Hello World!

๐Ÿ”น Get and Clean Buffer

Retrieve buffered content and clear the buffer in one operation. This is useful when you want to capture output without sending it to the browser.

<?php
ob_start();

echo "This content is captured";
echo " and stored in a variable.";

// Get contents and clean buffer
$captured = ob_get_clean();

// Now we can use the captured content
echo "Captured: " . $captured;
echo "<br>Length: " . strlen($captured) . " characters";
?>

Output:

Captured: This content is captured and stored in a variable.
Length: 51 characters

๐Ÿ”น Modifying Output

Capture output, modify it, and then send the modified version to the browser. This technique is useful for post-processing HTML or adding wrappers.

<?php
ob_start();

echo "hello world";

// Get and modify buffer
$content = ob_get_clean();
$content = strtoupper($content);
$content = str_replace("WORLD", "PHP", $content);

echo $content;
?>

Output:

HELLO PHP

๐Ÿ”น Nested Buffering

PHP supports multiple levels of output buffering. You can nest buffers to capture and manipulate output at different stages of your script execution.

<?php
// Start first buffer
ob_start();
echo "Outer buffer ";

// Start second buffer
ob_start();
echo "Inner buffer";
$inner = ob_get_clean();

echo "- " . strtoupper($inner);
$outer = ob_get_clean();

echo "Result: " . $outer;
?>

Output:

Result: Outer buffer - INNER BUFFER

๐Ÿ”น Buffer Callback Function

Apply a callback function to automatically process buffered output before it's sent. This is perfect for compression, minification, or content transformation.

<?php
// Callback function to modify output
function modifyOutput($buffer) {
    return str_replace("apple", "orange", $buffer);
}

// Start buffering with callback
ob_start("modifyOutput");

echo "I like apple juice.";
echo " Apple pie is delicious.";

// End buffering (callback is applied)
ob_end_flush();
?>

Output:

I like orange juice. orange pie is delicious.

๐Ÿ”น Checking Buffer Status

Monitor the state of output buffering in your script. These functions help you determine if buffering is active and how many buffer levels exist.

<?php
// Check buffer level
echo "Buffer level: " . ob_get_level() . "<br>";

ob_start();
echo "Buffer level: " . ob_get_level() . "<br>";

ob_start();
echo "Buffer level: " . ob_get_level() . "<br>";

// Get buffer length
echo "Buffer size: " . ob_get_length() . " bytes";

ob_end_clean();
ob_end_clean();
?>

Output:

Buffer level: 0
Buffer level: 1
Buffer level: 2
Buffer size: 45 bytes

๐Ÿ”น Practical Use: Redirects

Output buffering prevents "headers already sent" errors when you need to redirect after outputting content. Buffer the output to send headers at any time.

<?php
ob_start();

echo "Processing...";

// Some processing logic
$success = true;

if ($success) {
    ob_end_clean(); // Clear buffer
    header("Location: success.php");
    exit;
}

ob_end_flush(); // Send buffer if no redirect
?>

Output:

(Redirects to success.php)

๐Ÿง  Test Your Knowledge

Which function gets buffer content and clears it?