ob_get_length Buffer and Acquisition Buffer Length Instance in php
- 2021-08-05 08:59:34
- OfStack
This paper gives an example of ob_get_length buffer in php and the method of obtaining buffer length. Share it for your reference. The specific methods are as follows:
The file_get_contents () function reads the entire file into a string, just like file (), except that file_get_contents () reads the file into a string.
The file_get_contents () function is the preferred method for reading the contents of a file into a string, and memory-mapped techniques are used to enhance performance if supported by the operating system.
Syntax: file_get_contents (path, include_path, context, start, max_length)
ob_start(); // Open buffer
echo "hello"; // Output content
$out1= ob_get_contents(); // Get buffer contents
echo "world"; // Output content
$out2=ob_get_contents(); // Get the buffer contents again
ob_end_clean(); // Empty the buffer and close
echo $out1; // Output number 1 Second obtained result
echo "<br>";
echo $out2; // Output number 2 The results obtained in order to compare
This code is used when the output buffer is set to on (output_buffering=on)
List the output header information: print_r (ob_list_handlers ());
Refresh the buffer data, return the data and close the buffer: $buffer=ob_get_flush ();
Write buffer data to file: file_put_contents ('buffer. txt', $buffer);
List the output header information: print_r (ob_list_handlers ());
Get the buffer length, the example code is as follows:
// Open buffer
ob_start();
// Output content
echo "hello ";
// Get buffer length
$len1=ob_get_length();
// Re-output content
echo "world";
// Get the length of the buffer again
$len2=ob_get_length();
// Empty the buffer and close the buffer
ob_end_clean();
// Output number 1 Length of secondary acquisition
echo $len1;
echo "<br>";
// Output number 2 The length of the acquisition to compare two different results
echo $len2;
I hope this article is helpful to everyone's PHP programming.