What's the Use of Ob_Start() in PHP

What's the use of ob_start() in php?

Think of ob_start() as saying "Start remembering everything that would normally be outputted, but don't quite do anything with it yet."

For example:

ob_start();
echo("Hello there!"); //would normally get printed to the screen/output to browser
$output = ob_get_contents();
ob_end_clean();

There are two other functions you typically pair it with: ob_get_contents(), which basically gives you whatever has been "saved" to the buffer since it was turned on with ob_start(), and then ob_end_clean() or ob_flush(), which either stops saving things and discards whatever was saved, or stops saving and outputs it all at once, respectively.

usage of ob_start function

output buffering takes what is echoed between ob_start() and ob_get_clean() (or other output buffering ends) and prevents it from "early leaking" into the response that php serves to the user.

If you remove output buffering, the content of the response is just output "conventionally", however the $response object would be empty, $reponse->send() would fail, because you already output other stuff, so headers can't be sent anymore.

So, it works, because you either send the output directly (php's default mode of operation), or you buffer the ouput and send that output "indirectly" via the response object (or by just outputting the return value of ob_get_clean).

the main difference in my opinion is that the framework stuff, that might or might not add some utility to a response, can't do anything anymore, because the framework doesn't know what output you've send. the full symfony framework for example displays a profiler bar at the bottom, which it surely does by injecting it in the response. By circumventing the $response object, you also deny the injection. however, in your particular case, it might not matter at all.

What is advantages of using ob_start() over don't using it?

The effect on performance is negligible.

Normally PHP renders line by line as instructions are executed. however once you have output buffering turned on by using ob_start() it means that php will buffer the output and not render it until you hit ob_end_flush()

This is used in case you need to do more processing before sending down the output to the client.

However...

Although output buffering does not affect the performance, you can use it cleverly to increase your website performance. have a look at here

What is the purpose of the ob_ functions in php? (ob_start(), ob_get_contents(), etc.)

If you say this in PHP:

echo 'Hello';

it will result in the string Hello being sent to the browser more or less immediately. This is fine in contexts where you're outputting stuff directly, but sometimes you want to use that workflow, but capture the output instead of sending it to the browser. So instead, you can do this:

ob_start();
echo 'Hello';
$tmp = ob_get_contents();
ob_end_clean();

This will result in nothing being sent to the browser, but $tmp will now contain Hello.

This is useful in email because what you might want to do is render a template to a string, and then use that as an email message body rather than sending it to a browser.

With PHPMailer, you might use that workflow to create a message body, and then pass it to PHPMailer:

$mail->Body = $tmp;

The example you posted is a bit strange, because the mail() function doesn't typically output anything anyway, so it will capture nothing.

what is the role of ob_start() in here

Normally php sends all text not included in <?php ... ?>, all echos, all prints to the output. Which is send to the err... output: http server (which sends it to a client), console etc.

After ob_start this output is saved in output buffer, so you can later decide what to do with it.

It doesn't affect db connection. It deals with text (mostly) produced by php.

Whats the point of using ob_start without any parameters in PHP

One reason, is to "grab" the output of a small section of code.

So, let's say that you have an independent piece of code that you want to execute, but you don't want to just output it directly. What you can do, is

ob_start();
include 'file.php';
$output = ob_get_clean();

I'll give you a real world example. Say you are building an installer for an application. And as part of that installer you want to show the current PHP information (the data from phpinfo()). But, you want to integrate that information with the rest of the page (rather than use a frame). So, what you can do is grab the output of phpinfo() with an output buffer, modify it, and then display it where you want in your template file.

ob_start();
phpinfo();
$info = ob_get_clean();
//Massage the output, remove the doctype, html, head, and body tags
echo $info;

I also use it with view files. In the View class, the __toString() method actually renders the view. But since __toString() is expected to return a string rather than echo it, I use output buffering to capture the template and return it...



Related Topics



Leave a reply



Submit