How does ob_get_contents work in Php?
Asked Answered
P

4

17

This is a sample code from the book I am reading:

ob_start();
include("{$path}.ini");
$string = ob_get_contents();
ob_end_clean();
$pairs = parse_ini_string($string);

My question is, how does ob_get_contents() know what to get contents from? ({$path}.ini in this situation)?

Peterpeterborough answered 3/2, 2013 at 11:28 Comment(2)
Look at this :php.net/manual/en/function.ob-get-contents.phpHarijan
@OneManCrew I have already, but I could not find any useful information.Peterpeterborough
S
15

ob_get_contents simply gets the contents of the output buffer since you called ob_start(). Essentially, an output buffer in PHP catches anything that would have been output to the browser (excluding headers). It's useful in cases where you may need to filter some output, or you're using a PHP method (such as var_dump) that writes output directly to screen, and you would instead like the return value of the method in a string.

In this case, because you're include()ing the .ini file, it's contents will be essentially output to screen, and ob_get_contents() will get the content of the file.

If you were to put echo "I'm a little teapot short and stout"; underneath the include, this would also be included in $string after the body of the .ini file.

In your specific case, however, output buffering is an unnecessary overhead, simply use file_get_contents on the .ini file. I'm not sure why a book would even have this code in it at all.

Sloven answered 3/2, 2013 at 11:31 Comment(5)
Just a quick question, when you say "because you're include()ing the .ini file, it's contents will be essentially output to screen", but I do not see that contents of the path.ini on the screen?Peterpeterborough
... because you're using output buffering, that's kindof the point. ob_end_clean() removes the contents of the output buffer (which are now in $string), and it will never be output to the screen unless you echo $string;.Sloven
@KorayTugay When you remove ob_start() function, you will see it on the screen.Fubsy
@Burgos Thanks, I was able to see after I remove ob_end_clean(); not ob_start()... Strange..Peterpeterborough
Because ob_start() only begins the capture, if you don't clear (with ob_end_clean()) the buffer, it will be flushed to your screen at the end of the script.Sloven
M
6

The "ob" stands for "output buffer". When you call ob_start(), PHP reroutes all output (using echo, etc) to the output buffer. Then you can use the other ob_* functions to retrieve and/or clear the buffer contents.

In your example, it will buffer any output generated by the file referenced by "{$path}.ini". When you include it, its output is added to the buffer, and when you call ob_get_contents(), it retrieves the contents of the buffer.

Martinsen answered 3/2, 2013 at 11:32 Comment(0)
P
5

From PHP:

ob_start — Turn on output buffering
ob_get_contents — Return the contents of the output buffer
ob_end_clean — Clean (erase) the output buffer and turn off output buffering

Now, ob_get_contents can collect all buffer that outputted.

[1] http://www.php.net/manual/en/book.outcontrol.php

Plastid answered 3/2, 2013 at 11:32 Comment(2)
What is included in "buffer that outputted?"Peterpeterborough
The output is not printed out instead it saved in internal buffer, then, php can collect this output and save it in stringPlastid
F
2

ob_get_contents() is getting everything that is echoed after calling ob_start() function, so there is not anything special about {$path}.ini - you are required to echo data you want to collect (yes, even outputs of simple echo or print_r calls will be collected - sometimes useful for debugging simple scripts).

You may understand ob_start() function as a simple redirection from screen to (invisible) PHP internal buffer which is later read by ob_get_contents(). So you will be able to redirect anything that you may see on the screen without calling ob_start() function (even the whole web pages).

Fubsy answered 3/2, 2013 at 11:34 Comment(4)
but {$path}.ini is never echoed?Peterpeterborough
@KorayTugay The contents of it are, you called include.Sloven
@RudiVisser Besides echo, print_r, and include, what else is considered echoed?Peterpeterborough
@KorayTugay anything you can see in the browser without calling ob_start() function. You may understand it as a simple redirection data from screen to some container.Fubsy

© 2022 - 2024 — McMap. All rights reserved.