Capturing echo output and storing it in a variable
by Jay on Jul.06, 2011, under PHP
Working with legacy code somethings throws up little eggs that need to be cracked.
For instance, I need to use a function which is generally called via the browser and instead of using return statements, it uses echo.
This post shows you how to deal assign echo statements into a local variable by using output control.
ob_start();
helloWorld(param1, param2);
$capturedVar = ob_get_contents();
ob_end_clean();
The echo from the function helloWorld() is now stored in $capturedVar for use!