Is there any difference between these two pieces of PHP?
ob_start();
//code...
$pageContent = ob_get_contents();
ob_end_clean();
someFunction($pageContent);
There is one teeny difference between
$stuff = ob_get_clean();
and
$stuff = ob_get_contents();
ob_end_clean();
which is that the latter will throw an E_NOTICE
if there is no active output buffer at the time that you call it, and the former won't. Throwing the notice actually seems like the saner behaviour to me, since if you're calling these functions without an output buffer then you're probably doing something wrong!
That the two approaches are pretty much equivalent is explicitly documented on php.net, which says:
ob_get_clean()
essentially executes bothob_get_contents()
andob_end_clean()
.
The warning-throwing behaviour of ob_end_clean
is also documented:
If the function fails it generates an
E_NOTICE
.
Note that there is no similar sentence in the docs of ob_get_contents or ob_end_clean.
If you really want to assure yourself there are no further differences between these functions (there aren't), you can dive into the definitions of ob_get_contents, ob_end_clean and ob_get_clean in the source. There's some weird error handling for impossible cases in ob_get_clean
that should never get reached, but besides that, you can see that the behaviours are as described.