ob_get_contents + ob_end_clean vs ob_get_clean

前端 未结 4 1465
星月不相逢
星月不相逢 2020-12-15 16:28

Is there any difference between these two pieces of PHP?

ob_start();
//code...
$pageContent = ob_get_contents();
ob_end_clean();
someFunction($pageContent);
         


        
4条回答
  •  醉酒成梦
    2020-12-15 16:55

    To answer your question:

    ob_get_clean() essentially executes both ob_get_contents() and ob_end_clean().

    Yes. It is functionally equivalent.


    Case 1:

    ob_get_contents() + ob_end_clean():

    ob_get_contents — Return the contents of the output buffer

    ob_end_clean — Clean (erase) the output buffer and turn off output buffering

    So, basically, you're storing the contents of the output buffer to a variable and then clearing it with ob_end_clean().

    Case 2:

    ob_get_clean — Get current buffer contents and delete current output buffer

    You're storing the buffer contents to a variable and then the output buffer is deleted.


    What you're doing is essentially the same. So, I don't see anything wrong with using the second code-block here, since they're both doing the same thing.

提交回复
热议问题