PHP: Suppress output within a function?

后端 未结 4 2016
[愿得一人]
[愿得一人] 2020-12-08 19:28

What is the simplest way to suppress any output a function might produce? Say I have this:

function testFunc() {
    echo \'Testing\';
    return true;
}
         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 20:02

    Yes, messing with the Output Buffer is exactly the answer. 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):

    function foo() {
      echo "Flush!";
      return true;
    }
    
    ob_start();
    $a = foo();
    ob_end_clean();
    

    And no output is generated.

提交回复
热议问题