Heya, Pplers.
Do the functions that generate the HTML return it or echo it?
If they return the HTML, then you can simply use a variable to store the return values of your functions and then echo it when you are done.
If your functions output the HTML, then you'll have to use output buffering.
Note that you never actually explicitly call checkForSomething(). Instead, you pass it as a callback to
ob_start(). When you call
ob_end_flush(), PHP will automatically call checkForSomething() and pass all of the HTML that it was buffering (everything that your functions echoed) as $str.
Here's a simple example:
-
function processOutput($str)
-
{
-
return '<strong>' . $str . '</strong>';
-
}
-
-
ob_start('processOutput');
-
-
echo 'Hello, World!';
-
-
// Note that NOTHING has been output yet; it's in the output buffer.
-
-
ob_end_flush(); // Here is where the text gets output.
-
When this code executes, PHP turns on output buffering so that anything we echo, print, etc., gets put into a temporary buffer. When we call ob_start(), we assign a callback to the processOutput() function.
When we echo 'Hello, World!', because output buffering is active, instead of sending the text to the browser, we save the text in the output buffer instead.
Then when we call ob_end_flush(), the callback (processOutput()) gets called, and PHP passes all of the buffered text ('Hello, World!') to it.
processOutput() takes $str ('Hello, World!') and surrounds it with HTML strong tags. Then it returns the resulting string, which ob_end_flush() then sends to the browser.
The end result:
-
<strong>Hello, World!</strong>
-