PHP ob_get_clean() Function
Last Updated :
18 Aug, 2023
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
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
<?php
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();
$object = new GFG();
$object -> GFG_Funcion();
$saved_ob_level = ob_get_level();
$start_ob_level = "" ;
while (ob_get_level() > $start_ob_level ) {
ob_end_flush();
}
$content = ob_get_clean();
?>
|
Output:
A => Welcome
B => GeeksforGeeks
C => Geeks
Reference: https://www.php.net/manual/en/function.ob-get-clean.php
Share your thoughts in the comments
Please Login to comment...