Seo Forum

Coding & Programming => Programming Forum => Topic started by: chinmay.sahoo on 03-22-2016, 04:43:51

Title: What is the purpose of ob_start() ?
Post by: chinmay.sahoo on 03-22-2016, 04:43:51
OB_start() turn on the output buffer. While the buffer is on, no output is sent rather it is stored in the internal buffer. Inorder to get content from output Ob_get_content function can be used.
Title: Re: What is the purpose of ob_start() ?
Post by: TomClarke on 05-16-2016, 00:58:52
The OB_start() function activates the output buffer, which means that any output generated by PHP will be stored internally rather than sent directly to the browser. This allows the output to be manipulated or processed before it is sent. The content stored in the output buffer can be retrieved using the Ob_get_content() function.

Here are a couple of examples to illustrate the usage of output buffering in PHP:

Example 1: Using OB_start() and Ob_get_content()

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

// Generate some output
echo "Hello, this is a test.";

// Get the content from the output buffer
$output = ob_get_contents();

// End and clean the output buffer
ob_end_clean();

// Manipulate or use the output as needed
echo strtoupper($output); // Output will be: HELLO, THIS IS A TEST.
?>


Example 2: Using OB_start() to capture output in a variable

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

// Generate some output
echo "This will be captured in a variable.";

// Get the content from the output buffer and store it in a variable
$output = ob_get_contents();

// End and clean the output buffer
ob_end_clean();

// Now $output contains the captured output
echo $output; // Output will be: This will be captured in a variable.
?>


In both examples, the OB_start() function initiates output buffering, and the Ob_get_contents() function is used to retrieve the buffered output for further manipulation.


Here are a few more examples to further illustrate the usage of output buffering in PHP:

Example 3: Using OB_start() to include output from a file

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

// Include a file that generates output
include 'somefile.php';

// Get the content from the output buffer and store it in a variable
$output = ob_get_contents();

// End and clean the output buffer
ob_end_clean();

// Now $output contains the captured output from the included file
echo $output;
?>


Example 4: Using OB_start() to buffer JSON output

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

// Generate some JSON output
$data = array('name' => 'John', 'age' => 30);
echo json_encode($data);

// Get the JSON content from the output buffer
$jsonOutput = ob_get_contents();

// End and clean the output buffer
ob_end_clean();

// Manipulate or use the JSON output as needed
var_dump(json_decode($jsonOutput)); // Output will be: object(stdClass)#1 (2) { ["name"]=> string(4) "John" ["age"]=> int(30) }
?>


In these examples, the OB_start() function is used to initiate output buffering, and then the output is manipulated or retrieved using the appropriate functions before ending and cleaning the buffer with ob_end_clean().