PHP ob_get_clean() Function
Last Updated :
18 Aug, 2023
Improve
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
// Create an output buffer
ob_start();
echo "Welcome to GeeksforGeeks";
$out = ob_get_clean();
$out = strtolower($out);
var_dump($out);
?>
<?php
// Create an output buffer
ob_start();
echo "Welcome to GeeksforGeeks";
$out = ob_get_clean();
$out = strtolower($out);
var_dump($out);
?>
Output:
string(24) "Welcome to GeeksforGeeks"
Example 2:
<?php
// Declare a class
class GFG {
public function GFG_Funcion() {
$variable = array(
"A" => "Welcome",
"B" => "GeeksforGeeks",
"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();
?>
<?php
// Declare a class
class GFG {
public function GFG_Funcion() {
$variable = array(
"A" => "Welcome",
"B" => "GeeksforGeeks",
"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 => GeeksforGeeks C => Geeks
Reference: https://www.php.net/manual/en/function.ob-get-clean.php