PHP Tutorial #22 The Output_Buffer And How To Control It

Image Source

Repository

https://github.com/php/php-src  

What Will I Learn?

I will learn the output_buffer , how we can control it using the ob functions to start, get and empty contents in buffer.

  • What's the output_buffer.
  • How to start the buffer using 'ob_start' method.
  • How to empty the buffer using 'ob_end_clean' and 'ob_end_flush' methods.
  • How to get the content before closing the buffer using 'ob_get_contents' and 'ob_get_clean' methods.

Requirements

System Requirements:
  • Server support PHP , Xampp or Wamp for example
  • An IDE like Sublime text.
  •  Browser (Chrome for example)  
OS Support for PHP
  • Windows
  • macOS
  • Linux

Required Knowledge

  • HTML language
  • CSS language

Difficulty

  • Basic

Description

In this tutorial we will learn the output buffering or output buffer, when the developer triggers a display in PHP using (echo, print, printf, print_r ..etc) the thing we will display or print will not be displayed directly , it is actually stored in batteries called buffer, that the developer can easily control them.

The data will be displayed from the buffer, once it's emptied the data will be displayed.

We will talk about the buffer, the layers how to control them and the effects on our code or our display.

1- What's the output_buffering ?

The output buffer called "output_buffering" buffers everything that goes to the display from PHP, this layer is configurable via the "output_buffering" parameter of php.ini.

This parameter can take the values " on "," off " or an integer representing the size of the buffer in bytes. If " on " is chosen the size of 40960 bytes (40KB) is used.

If the value is " off " all the data will be transmitted to the SAPI, the SAPI is a buffer under the PHP buffer, when PHP empties its buffer it empties it in the SAPI which also has a buffer whose size not configurable via PHP.

This buffer can hold the data before displaying it.

It is possible to ask the SAPI layer to empty its buffer, this is done in several ways in PHP.

2- The output_buffer functions :

  a- Ob_Start

 This function starts the output_buffer, as long as it is triggered no data is sent to the browser but temporarily buffered.

 Output buffers are stacked, you can call ob_start() multiple times at the same time, make sure that you call ob_end_flush() often enough, if multiple callback functions are active the contents will be filtered sequentially in the order of nesting.

 This function returns TRUE on success or FALSE on error.

When we use ob_start() method , we can use echo and it will be displayed directly because the output_buffer is filled by the value that we have printed.

The length of the string is the length of the output_buffer, there is a function to get the length of the buffer is ob_get_length() , and this is the result

 b-  Ob_End_Clean() 

 This function clears the contents of the first output buffer and disables output buffering, if you want to process the contents of the buffer you will need to call ob_get_contents() before ob_end_clean(), because the buffer is destroyed by this method.

 It returns TRUE on success or FALSE on failure, the reasons for such a failure are that the output buffering might not be enabled.

 If the function fails, it generates an E_NOTICE note.

I will continue in the same example I will use echo to print "This value will be displayed directly on the browser" , after that I will use the ob_end_clean() function

The result is empty, so we lost the value that we want to display on the browser, and for that we must use the function ob_get_contents() before emptying and before losing the data.

 c- Ob_End_Flush  

 Sends the contents of the output buffer and turns " off " the output buffering, if you want to continue manipulating the buffer value you can call ob_get_contents() before ob_end_flush() but this function will already send the display to SAPI.

 This function is similar to ob_get_flush(), except that ob_get_flush() returns the buffer as a string.

 This function returns TRUE on success or FALSE on failure, the reasons for failure are that you might have called the function without having an active buffer.

 If the function fails, it issues an E_NOTICE alert.

The difference between ob_end_clean() and ob_end_flush() the both will empty the buffer but the flush will display the data on the browser, and this is the result

 d- Ob_Get_Content  

 Returns the contents of the output_buffer without erasing it or FALSE, if the output buffering is not enabled.

 If we want to display a character string for example but we have a session or a cookie, we will empty the buffer but before we must first get the content or the string of character and for that we use the function ob_get_content().  

Before using the ob_end_clean() method or before empty the output_buffer I need to get the content that I want to display first

I have created a variable " $value " , I will get the content of the ' ob ' and store it in this variable , after that I will use echo to print the result 

 e- Ob_Get_Clean  

 This function and the combination between the function ob_get_content() and ob_end_clean, that is, this function will first get the buffer contents and then finish the buffering session that was started by the ob_start () function.

 If buffering is not enabled, then FALSE will be returned.

The ob_get_clean() method minimizes the code, instead of writing ob_get_contents() and ob_end_clean() we will combine them and directly use ob_get_clean() and it will give the same result.

We will get the same result on the browser

Video Tutorial  

Curriculum

Proof of Work Done

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/RC6.php

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/RC6I.php

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/RC6O.php

Sort:  

Hi @alexendre-maxim

Thank you for your contribution to the video category.

I can see that you are making effort with your diction in the presentation of the tutorial so that your words are head with clear pronunciation.

Sometimes when you move from one concept to another, you can take a little pause.

You are doing good work, therefore you can consider doing more intermediate tutorials to bring your students to learn more complex concepts.

Looking forward to your next tutorial.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you for your review, @rosatravels! Keep up the good work!

Thank you @rosatravels, I'm going to raise the level more

Hi @alexendre-maxim!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Congratulations @alexendre-maxim! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 100 comments. Your next target is to reach 200 comments.

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Vote for @Steemitboard as a witness and get one more award and increased upvotes!

Hey, @alexendre-maxim!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!