Beginners Logo
  • होम
  • ताज़ा खबरें
  • करियर गाइडेंस
  • जॉब ओपनिंग्स
  • एडमिशन
  • परीक्षा की जानकारी
  • एडमिट कार्ड
  • रिजल्ट
  • सामान्य ज्ञान
  1. Home
  2. Tutorial
  3. Php
  4. Php Output Control

PHP Output Control

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(): Turn on output buffering.
  • ob_get_contents(): Return the contents of the output buffer.
  • ob_end_clean(): Clean (erase) the output buffer and turn off output buffering.
  • ob_end_flush(): Send the contents of the output buffer and turn off output buffering.
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_flush(): Flush the output buffer, but keep buffering active.
  • flush(): This tries to push current output all the way 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_get_level(): Return the nesting level of the output buffering mechanism.
  • ob_get_length(): Return the length of the output buffer.
  • ob_get_clean(): Get current buffer contents and delete current output buffer.
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

UPCET Exam

Click Here

SAAT Exam

SAAT Exam

Click Here

MHT CET Exam

MHT CET Exam

Click Here

IPU CET Exam

IPU CET Exam

Click Here

KCET Exam

KCET Exam

Click Here

COMEDK UG Exam

COMEDK UG Exam

Click Here

VITEEE Exam

VITEEE Exam

Click Here

BITSAT

BITSAT

Click Here

DSAT: Dayanand Sagar Admission Test

DSAT: Dayanand Sagar Admission Test

Click Here

Career In Animation in india

Career In Animation in india

Click Here

Merchant Navy Courses in india

Merchant Navy Courses in india

Click Here

Interior Design Career in india

Interior Design Career in india

Click Here

UGC NET Exam

UGC NET Exam

Click Here

B. Ed Exam

B. Ed Exam

Click Here

AFCAT - Air Force Common Admission Test

AFCAT - Air Force Common Admission Test

Click Here

GATE Exam

GATE Exam

Click Here

Joint Entrance Examination (JEE)

Joint Entrance Examination (JEE)

Click Here

Common Admission Test (CAT)

Common Admission Test (CAT)

Click Here

CDS - Combined Defence Services Exam

CDS - Combined Defence Services Exam

Click Here

अन्य खबरें

  • प्राइवेट नौकरी अपडेट: Testbook में कंटेंट राइटर की नई वैकेंसी, ग्रेजुएट करें अप्लाई, दिल्ली में मिलेगी फुल टाइम जॉब

  • मार्च में सरकारी नौकरी के सुनहरे मौके! बैंक, पुलिस, डाक विभाग से लेकर असिस्टेंट प्रोफेसर तक कई भर्तियाँ जारी

  • केवीएस बाल वाटिका एडमिशन 2025: केंद्रीय विद्यालय में नर्सरी, एलकेजी, यूकेजी में प्रवेश शुरू, आवेदन प्रक्रिया और अंतिम तिथि जानें

  • नीट यूजी 2025 परीक्षा रजिस्ट्रेशन शुरू: फीस, एडमिट कार्ड, परीक्षा तिथि और रिजल्ट से जुड़ी सभी जरूरी जानकारी

  • यूपी बीएड प्रवेश परीक्षा 2025: आवेदन की तारीख घोषित, जानें रजिस्ट्रेशन प्रक्रिया, परीक्षा तिथि और अन्य जरूरी जानकारी

  • Bihar DElED Exam 2025: डीएलएड अभ्यर्थियों के लिए जरूरी अपडेट, आवेदन में हुई गलती ऐसे करें सुधार, अंतिम तिथि से पहले पूरा करें प्रक्रिया

  • PHP Tutorial
    • PHP Tutorial
    • PHP Introduction
    • PHP Installation
    • PHP Syntax
    • PHP Comments
    • PHP Variables
    • PHP Echo / Print
    • PHP Data Types
    • PHP Strings
    • PHP Numbers
    • PHP Math
    • PHP Constants
    • PHP Operators
    • PHP If...Else...Elseif
    • PHP Switch
    • PHP Loops
    • PHP Functions
    • PHP Arrays
    • PHP Superglobals
    • PHP RegEx
    • PHP Forms
    • PHP Form Handling
    • PHP Form Validation
    • PHP Form Required
    • PHP Form URL/E-mail
    • PHP Form Complete
    • PHP Advanced
    • PHP Date and Time
    • PHP Include
    • PHP File Handling
    • PHP File Open/Read
    • PHP File Create/Write
    • PHP File Upload
    • PHP Cookies
    • PHP Sessions
    • PHP Filters
    • PHP Filters Advanced
    • PHP Callback Functions
    • PHP JSON
    • PHP Exceptions
    • PHP OOP
    • PHP What is OOP
    • PHP Classes/Objects
    • PHP Constructor
    • PHP Destructor
    • PHP Access Modifiers
    • PHP Inheritance
    • PHP Abstract Classes
    • PHP Interfaces
    • PHP Traits
    • PHP Static Methods
    • PHP Static Properties
    • PHP Namespaces
    • PHP Iterables
    • MySQL Database
    • MySQL Connect
    • MySQL Create DB
    • MySQL Create Table
    • MySQL Insert Data
    • MySQL Get Last ID
    • MySQL Insert Multiple
    • MySQL Prepared
    • MySQL Select Data
    • MySQL Where
    • MySQL Order By
    • MySQL Delete Data
    • MySQL Update Data
    • MySQL Limit Data
    • AJAX Intro
    • AJAX Database
    • AJAX Live Search
    • AJAX Poll
    • PHP Examples
    • PHP Compiler (Editor)
    • PHP Quiz
    • PHP Exercises
    • PHP Certificate
    • PHP Reference
    • PHP Overview
    • PHP Array
    • PHP Calendar
    • PHP Date
    • PHP Directory
    • PHP Error
    • PHP Exception
    • PHP Filesystem
    • PHP Filter
    • PHP FTP
    • PHP Keywords
    • PHP Mail
    • PHP Misc
    • PHP MySQLi
    • PHP Network
    • PHP Output Control
    • PHP Stream
    • PHP String
    • PHP Variable Handling
    • PHP XML Parser
    • PHP Zip
    • PHP Timezones
  • Other Tutorials
    • Angular
    • CSS
    • Javascript
    • Mysql
    • Node JS
    • PHP Tutorial
    • Python
    • React Tutorial