Output Control Functions

PHP provides a set of functions that control what content is sent to the browser and when. This is referred to as output control.

Output can come from any of the following sources:

  • echoprintprintfprint_r… and other similar functions and statements
  • Notices, warnings and errors from PHP
  • Any content outside of the <?php ?> tags

PHP and the backend on which it is running may hold the output in a buffer before sending it to the user.

ob_get_contents() Function

The ob_get_contents() function returns the contents of the topmost output buffer.

Example

<?php
ob_start();
echo "Hello World!";
$contents = ob_get_contents();
ob_end_clean();
echo "The contents of the buffer are: ";
echo $contents;
?>