Reputation: 829
Obviously there are usability and readability differences, but are there any advantages or disadvantages to the methods below – especially performance-wise?
function method1() {
ob_start();
?><div>some html</div><?php
echo ob_get_clean();
}
function method2() {
?><div>some html</div><?php
}
function method3() {
echo '<div>some html</div>';
}
In particular between method1
and method2
, does the added ob_start()
cause a performance hit? Do I even need to use it when outputting HTML?
Upvotes: 1
Views: 620
Reputation: 324610
The "typical" use for ob_start()
is to buffer the output so that you can still use functions like setcookie
, header
and others without having to worry about "did I output something already?", it'll work. Honestly I think that should be default behaviour but that's just me.
ob_start()
gains real power when you give it a callback, however. I personally have this:
ob_start(function($html) {
return preg_replace('/\r?\n\t*/','',$html);
});
This strips out newlines (and subsequent tabs) from the HTML source before sending it to the browser, thus minifying it.
Upvotes: 1