suppressing output of a function in PHP. disabling echo just for a function in PHP

Have you ever had a situation where you had to call a function just to return its value without echoing anything ? May be disabling some errors or debugging messages ? I've been fiddling around with this for a while and I figured meddling with the Output Bugger might help.

function foo() {
echo "Flush!";
return true;
}

If you want to call foo() without echoing Flush! but you want $a to have the return value of foo().This is what you will do.

ob_start();
$a = foo();
ob_end_clean();

Just turn it on before you call your method that would output (not the function itself, but where you call it, you could wrap it around your whole script or the script flow, but you can make it as "tight" as possible by just wrapping it around the call of the method. For more information see http://php.net/outcontrol

No comments:

Post a Comment