php緩沖輸出實例分析

字號:


    本文實例講述了php緩沖輸出用法。分享給大家供大家參考。具體分析如下:
    ob_start([string output_callback])- 打開輸出緩沖區(qū)
    所有的輸出信息不在直接發(fā)送到瀏覽器,而是保存在輸出緩沖區(qū)里面,可選得回調函數(shù)用于處理輸出結果信息.
    ob_end_flush - 結束(發(fā)送)輸出緩沖區(qū)的內容,關閉輸出緩沖區(qū)
    實例代碼如下:
    復制代碼 代碼如下:ob_start(); //打開緩沖區(qū)
    echo "hello world"; //輸出內容
    $out=ob_get_clean(); //獲得緩沖區(qū)內容并且結束緩沖區(qū)
    $out=strtolower($out); //將字符轉換為小寫
    var_dump($out); //輸出結果
    //
    if(!function_exists('ob_clean')) //判斷函數(shù)是否被定義
    {
    function ob_clean() //定義函數(shù)
    {
    if(@ob_end_clean())
    {
    return ob_start();
    }
    trigger_error("ob_clean() failed to delete buffer.no buffer to delete.",e_user_notice);
    return false;
    }
    }
    //
    header('content-type: multipart/x-mixed-replace;boundary=endofsection'); //發(fā)送標頭
    print "n--endofsectionn"; //輸出內容
    $pmt=array("-","","|","/"); //定義數(shù)組
    for($i=0;$i<10;$i++) //通過循環(huán)進行操作
    {
    sleep(1); //暫停執(zhí)行
    print "content-type: text/plainnn"; //輸出內容
    print "part $it".$pmt[$i % 4]; //輸出內容
    print "--endofsectionn"; //輸出內容
    ob_flush(); //發(fā)送緩沖區(qū)數(shù)據(jù)
    flush(); //刷新輸出緩沖
    }
    print "content-type: text/plainnn"; //輸出內容
    print "the endn"; //輸出內容
    print "--endofsection--n"; //輸出內容
    希望本文所述對大家的php程序設計有所幫助。