In php, ob_get_length buffering and obtaining buffer length instances

Source: Internet
Author: User
This article mainly introduces the methods for getting the buffer length and ob_get_length in php. The example analyzes the usage and related skills of the buffer output, which has some reference value. For more information, see

This article mainly introduces the methods for getting the buffer length and ob_get_length in php. The example analyzes the usage and related skills of the buffer output, which has some reference value. For more information, see

This article describes how to buffer and obtain the buffer length of ob_get_length in php. Share it with you for your reference. The specific method is as follows:

The file_get_contents () function reads the entire file into a string. Like file (), the difference is that file_get_contents () reads the file into a string.

The file_get_contents () function is the preferred method for reading the file content into a string. If the operating system supports this function, the memory ing technology is used to improve the performance.

Syntax: file_get_contents (path, include_path, context, start, max_length)

The Code is as follows:

Ob_start (); // open the buffer
Echo "hello"; // output content
$ Out1 = ob_get_contents (); // obtain the buffer content
Echo "world"; // output content
$ Out2 = ob_get_contents (); // obtain the buffer content again
Ob_end_clean (); // clear the buffer and disable
Echo $ out1; // output the result obtained for the first time
Echo"
";
Echo $ out2; // output the second result to compare

This code is used when the output buffer is set to enable (output_buffering = on)

List the output header information: print_r (ob_list_handlers ());

Refresh the buffer data, return the data, and disable the buffer: $ buffer = ob_get_flush ();

Write the buffer data to the file: file_put_contents('buffer.txt ', $ buffer );

List the output header information: print_r (ob_list_handlers ());

Obtain the buffer length. The instance code is as follows:

The Code is as follows:

// Open the buffer
Ob_start ();
// Output content
Echo "hello ";
// Obtain the buffer Length
$ Len1 = ob_get_length ();
// Output content again
Echo "world ";
// Obtain the buffer length again
$ Len2 = ob_get_length ();
// Clear the buffer and disable the buffer.
Ob_end_clean ();
// Output the length obtained for the first time
Echo $ len1;
Echo"
";
// Output the length of the second query to compare two different results
Echo $ len2;

I hope this article will help you with PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.