Posted in Script

php buffer 的運用

php buffer 的運用 Posted on 2017 年 04 月 13 日Leave a comment

當我把結果都秀出在畫面上,但我仍可以讓它再另外放到變數裡,接著我可以 mail 結果,或其它用途

<?php

ob_start();

echo 1;

echo 2;

$r = ob_get_contents();

ob_end_clean();

$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=utf-8';
$body = '<pre>' . print_r($r) . '</pre>';
mail('cross@ssorc.tw', 'subject', $body, implode("\r\n", $headers));

引用 : https://stackoverflow.com/questions/8770910/difference-between-ob-clean-and-ob-flush

ob_start();
print "foo";      // This never prints because ob_end_clean just empties
ob_end_clean();   //    the buffer and never prints or returns anything.

ob_start();
print "bar";      // This IS printed, but just not right here.
ob_end_flush();   // It's printed here, because ob_end_flush "prints" what's in
                  // the buffer, rather than returning it
                  //     (unlike the ob_get_* functions)

依序輸入至螢幕上 (不是等全部跑完)
引用 : http://php.net/manual/zh/function.flush.php#116485

ob_clean();
while(true){
    echo str_repeat(' ', 1024*1024*4);
    echo 'hello, world ...
'; ob_flush(); flush(); usleep(1000); }

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *