What is an "output_buffer"?

Asked

Viewed 505 times

4

Reading is reply, I had this doubt.

What is a output_buffer ?

  • An output buffer is an output control to which you fill the buffer with the data you want to send to the client. Already the context of this question I think refers to avoid sending data about the page and send only the string concatenated.

  • So I’m learning php, and I think I’ll need to edit my question, to offer more elements relevant to my question... I saw that in php you have a set of ob_* functions, so I need to see how I’m going to edit so that the answer is more focused on the practical line in php... Peri, I’m giving a read to find the right words....

  • @Magichat has something I need to improve on the answer?

  • man, there’s a lot of your answer I need to read, but I like to evaluate well... I haven’t done it yet... soon I’ll do it

1 answer

5

Buffer

When we talk about buffer we’re talking about buffer data, not to be confused with the term used in other contexts even in computing.

It is a memory area reserved for temporary storage of data, in general when it will transport from one place to another.

The buffer can be a simple variable with a reserved amount of bytes or some more complex structure.

Buffer is an abstract concept, it always materializes through another mechanism, in general a variable.

Template

PHP works with a page feedback system. So anything that is not PHP code is text that will be sent to the HTTP server.

PHP codes can generate new texts dynamically inserted on the page, in general as echo or print.

PHP is actually a language that always generates text as output. And all the code does is determine what that code is.

In most languages a command print or echo would print the text on the screen. PHP can work that way too. But the most common is that it provides pages and other web elements to the HTTP server. It would make no sense to write on the screen.

Everything the code says to print, including the parts outside the tags <?php ... ?> is sent to the server.

Exit

To avoid this perforated thing and give more flexibility we can use a buffer to store everything that would be sent to the server and we can control when to send to the server, if we want this.

In the PHP library you have the option to connect and manipulate the contents of this buffer. That’s what was done in that reply.

Not much of a secret in the family of functions initiated by ob_ we have how to start the buffer, take the data and terminate by wiping or dropping the data to the server.

If you plan well you can do the same manually without these functions. Instead of just printing you are already manipulating a variable created specifically to be the buffer.

Documentation.

Header

When sending directly to the server you have to think about the order to do things. You can’t send something you need to enter before it’s already sent. For example you can’t send a header HTTP after you started sending the page itself.

With the buffer can. Just like using a variable to concatenate the entire output of script.

Example:

<?php
    ob_start();
    echo "1 : Hello\n";
    ob_start();
    echo "2 : Hello";
    var_dump(ob_get_clean());
    echo "3 : Hello";
?>

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.