Tuesday, November 19, 2024
Google search engine
HomeLanguagesPHP ob_get_clean() Function

PHP ob_get_clean() Function

The ob_get_clean() function is an in-built PHP function that is used to clean or delete the current output buffer. It’s also used to get the output buffering again after cleaning the buffer. The ob_get_clean() function is the combination of both ob_get_contents() and ob_end_clean().

Syntax:

string|false ob_get_clean();

Parameters: It does not accept any parameter.

Return value: This function returns the contents of the output buffer and end output buffering. If output buffering is not active, then it returns false.

Example 1: Below is a simple example of ob_get_clean() functionality.

PHP




<?php
  
 // Create an output buffer
ob_start();
  
echo "Welcome to neveropen";
  
$out = ob_get_clean();
$out = strtolower($out);
  
var_dump($out);
?>


Output:

string(24) "Welcome to neveropen"

Example 2:

PHP




<?php
  
// Declare a class 
class GFG {
    public function GFG_Funcion() {
        $variable = array(
            "A" => "Welcome",
            "B" => "neveropen",
            "C" => "Geeks"
        );
  
        foreach ($variable as $key => $value) {
            echo $key . " => " . $value;
            echo "<br/>";
        }
    }
}
  
ob_start();
  
// Creating an object of class GFG
$object = new GFG();
  
// Calling function
$object -> GFG_Funcion();
  
$saved_ob_level = ob_get_level();
  
$start_ob_level="";
  
while (ob_get_level() > $start_ob_level) {
    ob_end_flush();
}
  
// Now, it is the final output buffer
$content = ob_get_clean();
  
?>


Output:

A => Welcome
B => neveropen
C => Geeks

Reference: https://www.php.net/manual/en/function.ob-get-clean.php

RELATED ARTICLES

Most Popular

Recent Comments