How do I capture the result of an echo() into a variable in PHP?

前端 未结 4 735
攒了一身酷
攒了一身酷 2020-12-19 12:31

I\'m using a PHP library that echoes a result rather than returns it. Is there an easy way to capture the output from echo/print and store it in a variable? (Other text has

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-19 13:10

    Its always good practise not to echo data until your application as fully completed, for example

    
    

    now session_start along with another string of functions would not work as there's already been data outputted as the response, but by doing the following:

    
    

    This would work and its less error prone, but if its a must that you need to capture output then you would do:

    ob_start();
    
    //Whatever you want here
    
    $data = ob_get_contents();
    
    //Then we clean out that buffer with:
    ob_end_clean();
    

提交回复
热议问题