Teach you how to use the ob function to output static html files

巴扎黑
Release: 2023-03-13 15:52:01
Original
1591 people have browsed it

How to use the ob function to output static html files

1. Introduction to the ob function

1.1. ob_start - open the output control buffer

bool ob_start ([ callback $output_callback [, int $chunk_size [, bool $erase ]]] )
This function will open the output buffer. When output buffering is activated, the script will not output content (except http headers), instead the content to be output is stored in an internal buffer.
Reference for details:
#1.2, ob_get_contents - Return the contents of the output buffer

##string ob_get_contents ( void )Just get the output buffer area, but does not clear it. Reference for details:


##1.3, ob_end_flush — flush out (send out) the contents of the output buffer and close the buffer

##bool ob_end_flush ( void )This function will send the contents of the top-level buffer (if there is content inside) and close the buffer. If you want to further process the contents of the buffer, you must call ob_get_contents() before ob_end_flush(), because the buffer contents are discarded after calling ob_end_flush(). Reference for details:


##1.4, ob_flush - flush out (send) the contents of the output buffer

##void ob_flush ( void )This function will send out the contents of the buffer (if there is content inside if). If you want to further process the contents of the buffer, you must call ob_get_contents() before ob_flush(), because the buffer contents will be discarded after calling ob_flush(). This function will not destroy the output buffer, but functions like ob_end_flush() will destroy the buffer. Reference for details:



##1.5, ob_get_clean - get the contents of the current buffer and delete the current output buffer

#string ob_get_clean ( void )Get the contents of the current buffer and delete the current output buffer. ob_get_clean() essentially executes ob_get_contents() and ob_end_clean() together. Reference for details:


##1.6, ob_get_flush - Flush (send out) the buffer content, return the content in the form of a string, and close the output buffer

#string ob_get_flush ( void )ob_get_flush() flushes (sends out) the contents of the buffer, returns the contents as a string, and closes the output buffer.
Note: This function is similar to ob_end_flush(), except that this function also returns the buffer content in string form.
Reference for details:
##2. How to use the ob() function to create a static page of html

2.1. Simple output html file

#ob_start(); //Open buffering District$info = 'hello world! ! ';
$file=fopen('index.html','w'); //Open the file index.html
fwrite($file,$info); //Write information to index.html
fclose($file); //Close the file index.html
?>
Output hello to index.html


Find index.html, and output the setting normally content

2.2. Obtain database information and output html file

#require_once 'coon.php';$sql = "select * from name order by id;";$result = $ link->query($sql);
$arr ​​= array();
while($re = $result->fetch(PDO::FETCH_ASSOC)){
$arr[] = $re;
}
//Loop output content to html file
ob_start(); //Open buffer
?>