Control output in PHP

PHP has functions to control the output of data to the client. You can store the output in a buffer, to send it to the client when you want.

As we know, PHP performs page processing and sends the result of processing the PHP code to the user’s computer. As a rule, as the page is being processed, the resulting HTML code is sent to the client, but these settings can be changed, even at runtime.

With PHP we can store the output, as it is generated, in a buffer. So that no data is sent to the client until it is expressly indicated. There are a number of functions that are used to achieve this behavior, which are the .

This behavior is very useful when you have to send information in the head of the page, after you have started to process the PHP code and have started to generate the output.

Note: As we should know, there are functions like header() or setcookie() that must be executed before any page text has been sent to the client. Otherwise, an error “Cannot modify header information – headers already sent by…” will be produced.

In this article we will see a small example of PHP code that buffers the output, to send it once all the code has finished processing.

We will use two functions that we may not know about, to control the output: ob_start() and ob_end_flush().

The ob_start() function is used to tell PHP to start buffering the output, that is, it should start saving the output to an internal buffer, instead of sending it to the client. So even if you write HTML code with echo or directly outside of PHP code, it won’t be sent to the browser until explicitly commanded. Or eventually, until the processing of the entire PHP file is finished.

See also  Web Developer, extension for Firefox

The ob_end_flush() function is used to tell PHP that you want to flush the entire buffer to the output, so it will be sent to the client that requested the page.

Let’s look at this code:

ob_start();

echo “”

?>





Page processed with output buffer





This is my page!!!





setcookie(“name”, “pepe”);

ob_end_flush();

?>

As soon as it starts, ob_start() is executed. This will save all output in a buffer. Now, when you write to the page, in the next line with the echo, and in the others, outside the PHP code, the only thing that happens is that the aforementioned buffer fills up.

Before terminating the page, in the next block of PHP code, a cookie is sent to the user’s browser. That cookie arrives without a problem and does not generate any errors, even though the code for the page has been written, since the code was not sent to the browser, but was stored in the buffer.

Finally, ob_end_flush() is called to flush the buffer to the output.

We can try commenting out the lines that execute the ob_start() and ob_end_flush() functions. Then we would see how the setcookie() function would cause an error, because this function cannot be executed if text has already been written to the page and therefore the http headers have already been sent to the client.

This is a very simple example of output control in PHP. But from here you can complicate everything you need. We hope this article will be an interesting introduction to continue investigating issues related to this basic functionality of PHP.

See also  Radial Gradients with CSS3

It may be interesting for you to continue with this topic, so I recommend that you read the article.

Loading Facebook Comments ...
Loading Disqus Comments ...