PHP Output Control allows developers to control what gets sent as output to the browser. This can be especially useful for buffering output or setting headers after some content has been generated. Here are the primary aspects of PHP's Output Control:
Output Buffering: When you turn on output buffering, no output is sent from the script (other than headers), instead, the output is stored in an internal buffer.
ob_start(); // Start buffering
echo "Hello, World!"; // This won't be immediately sent to the browser
$output = ob_get_contents(); // Get the buffer content
ob_end_clean(); // Clean the buffer
echo $output; // Outputs "Hello, World!"
Flushing the Output: In some cases, you might want to flush part of the buffered output to the browser.
ob_start(); // Start buffering
echo "Part 1 of my content.";
ob_flush(); // Flush this part to the browser
flush(); // Try to push it all the way to the browser
sleep(2); // Wait for 2 seconds
echo "Part 2 of my content.";
ob_end_flush();
Nested Output Buffering: You can have nested output buffering, meaning you can start a new buffer while another one is still active. Each ob_start() call needs its own ob_end_clean() or ob_end_flush().
ob_start();
echo "First level.";
ob_start();
echo "Second level.";
$second_level = ob_get_contents();
ob_end_clean();
ob_end_flush();
echo $second_level; // Outputs "Second level."
Output Control Functions:
ob_start();
echo "Hello, World!";
if (ob_get_level() > 0) {
echo "\nBuffering is active.";
echo "\nBuffer length: " . ob_get_length();
}
$content = ob_get_clean();
echo $content; // Outputs everything
Output Handlers:
PHP allows for the possibility to use callback functions with the output buffer. This lets you process the buffer's content before it's sent to the browser.
function callback($buffer) {
// Convert all the content to uppercase
return strtoupper($buffer);
}
ob_start("callback");
echo "This will be uppercased.";
ob_end_flush(); // Outputs "THIS WILL BE UPPERCASED."
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here