search

Home  >  Q&A  >  body text

comet - How to clear php cache area

The code is as follows

while(true){

        $getitem = mysql_query("select * from bulletin order by id desc limit 1");

        $item = mysql_fetch_array($getitem);

        echo json_encode($item,JSON_UNESCAPED_UNICODE);

        ob_flush();

        flush();

        ob_clean(); //Don’t quite understand the function of ob_clean

        mysql_data_seek($getitem,0);

        sleep(1);
}

The PHP cache area can output the contents of the cache area to the browser through ob_flush and flush, and the function of ob_clean is to clear the cache area, so the expected result is to output only the last piece of data each time. But in fact, the previous output is not cleared. How can I achieve my needs?

phpcn_u1582phpcn_u15822758 days ago472

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 13:12:26

    Usage of the following three functions

    ob_get_contents() - 返回输出缓冲区的内容
    ob_flush() - 冲刷出(送出)输出缓冲区中的内容
    ob_clean() - 清空(擦掉)输出缓冲区
    ob_end_flush() - 冲刷出(送出)输出缓冲区内容并关闭缓冲
    ob_end_clean() - 清空(擦除)缓冲区并关闭输出缓冲
    flush() - 刷新输出缓冲    
    通常是ob_flush();flush()同时一起使用
    使用ob_start()把输出那同输出到缓冲区,而不是到浏览器。
    然后用ob_get_contents得到缓冲区的数据。

    ob_start() opens a buffer on the server to save all output. So anytime echo is used, the output will be added to the buffer until the program ends or is terminated using ob_flush(). Then the content of the buffer in the server will be sent to the browser, which will be parsed and displayed by the browser.

    The function ob_end_clean will clear the contents of the buffer and close the buffer, but will not output the content.
    At this time, a function ob_get_contents() must be used in front of ob_end_clean() to obtain the contents of the buffer.
    In this case, the content can be saved to a variable before executing ob_end_clean(), and then the variable can be operated after ob_end_clean().

    reply
    0
  • Cancelreply