PHP 輸出緩衝

WBOY
發布: 2024-08-29 13:07:06
原創
451 人瀏覽過

PHP 輸出緩衝是一個確認 PHP 引擎保存資料的過程,同時在輸入進行處理時提供輸出。一旦 PHP 引擎獲得處理後的資料並執行以提供輸出,那麼同時該資料就會一點一點地發送到引擎並發送到瀏覽器。如果使用上述的輸出緩衝機制來執行,那麼這將在資料處理方面提供更高的效率和可行性,因為資料首先儲存在變數中,然後作為腳本的一部分發送到瀏覽器。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法:

輸出緩衝沒有固定的格式,但可以透過以下方式表示和使用:

<?php
function to start
php_info( ); // processing before giving the output.
use variable to assign the final value as an output
?>
登入後複製

輸出緩衝在 PHP 中如何運作?

PHP 中的輸出緩衝在其工作方面具有重要意義,如下所示:

  • 由於 PHP 是一種解釋性語言,輸出流引導的頁面很難輕鬆顯示。因此,正在採取一些舉措,例如輸出緩衝。
  • 輸出緩衝有助於將資料儲存到某個變數中,該變數將在執行 PHP 腳本後將請求傳送到瀏覽器之前用於渲染。
  • 如果頁麵包含在輸出緩衝及其各種功能中,則所有載入時扭曲且緩慢的頁面都會變得正常。
  • PHP 中的輸出緩衝有很多優點,其中之一是開發人員虔誠地使用這個PHP 函數,因為它減少了客戶端和伺服器之間的交互次數,作為一個整體,HTML立即發送到瀏覽器,從而使其對於較大規模的項目更加通用、靈活和高效,在這些項目中,大量頁面和組件作為大螢幕出現在圖片中。
  • 另一方面,還有一些更多的優點,因為整個輸出緩衝區使用許多其他變數作為HTML 中的字串整體存儲,所有HTML 檔案都可以使用string 方法和其他編寫的內建自訂方法進行操作和修改由程式設計師編寫,這反過來又有助於PHP 中網頁的流暢渲染。
  • 許多其他壓縮方法也可用於建立和操作更簡單的渲染方式。
  • Cookie 和會話管理在PHP 中的輸出緩衝方面也發揮著關鍵作用,因為它為PHP 中的輸出緩衝提供了額外的優勢,可以有效地獲取標頭信息,這些標頭信息不完全作為內容的一部分發送,但無論如何需要發送。
  • 此外,應該記住,在任何面向PHP 的應用程式中使用輸出緩衝之前,非常需要檢查與PHP 版本相關的兼容性問題,因為它可能根據要求使用其他PHP 版本,然後可能會出現PHP 輸出緩衝功能可能無法如預期般正常運作。因此,需要檢查輸出緩衝是否啟用,預設情況下它是關閉的。
  • 它還提供了一些資料庫呼叫方面的更多功能,因為它允許程式設計師使用一些高級功能,例如最小化和減少,這也適用於 cookie 和會話。
  • 輸出緩衝被認為是最安全、最高效的方法之一,對頁面渲染需要格外小心和能力,因為它提供了大多數最終用戶所期望的更快、靈活、更流暢、安全的方法。
  • 透過保存資料和各種操作來使用緩衝來暫時改進整個頁面導航和渲染的過程是最現代化的概念之一。

範例

讓我們討論 PHP 輸出緩衝的範例。

範例#1

此程式示範了由使用者定義的回呼() 函數,該函數將取代變數中定義的值,如輸出所示。

代碼:

<!DOCTYPE html>
<html>
<body>
<?php
function cll_bck($buff)
{
return (str_replace("Mobile", "Tabs", $buff));
}
ob_start("cll_bck");
?>
<html>
<body>
<p>Everyone_prefers_Mobile_over_Tabs.</p>
</body>
</html>
<?php
ob_end_flush();
?>
</body>
</html>
登入後複製

輸出:

PHP 輸出緩衝

範例#2

此程式示範了 ob_get_contents() 函數,用於取得定義到最終引擎的內容,同時傳遞輸出中所示的變數。

代碼:

<!DOCTYPE html>
<html>
<body>
<?php
ob_start();
echo "Today_day_is_good. ";
$o_t_1 = ob_get_contents();
echo "and_pleasant";
$o_t_2 = ob_get_contents();
ob_end_clean();
var_dump($o_t_1, $o_t_2);
?>
</body>
</html>
登入後複製

輸出:

PHP 輸出緩衝

Example #3

This program demonstrates the ob_start function where the output buffering gets initiated and then it gets displayed as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
ob_start();
echo 'Text written will_get displayed easily.';
?>
</body>
</html>
登入後複製

Output:

PHP 輸出緩衝

Example #4

This program demonstrates the use of text that will get removed once the ob_end_clean function is called as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
ob_start();
echo 'Text_written_will_get_removed_easily_using ob_end_clean.';
ob_end_clean();
?>
</body>
</html>
登入後複製

Output:

PHP 輸出緩衝

Example #5

This program demonstrates the ob_list_handlers() function which is used to return an array with the output buffer handler with the list of handlers as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
print_r(ob_list_handlers());
ob_end_flush();
ob_start("ob_gz_handler");
print_r(ob_list_handlers());
ob_end_flush();
ob_start(function($str_2)
{
return $str_2;
});
print_r(ob_list_handlers());
ob_end_flush();
?>
</body>
</html>
登入後複製

Output:

PHP 輸出緩衝

Example #6

This program demonstrates the encoding and decoding of all types of possible codes being defined but if in case something is missing, or the browser is getting some value as wrong then it will return the output as shown.

Code:

<!DOCTYPE html>
<html>
<body>
<pre class="brush:php;toolbar:false">
<?php
iconv_set_encoding("int_encd", "internal_UTF_8");
iconv_set_encoding("o/p_encd", "ISO-8859-1");
var_dump(iconv_get_encoding('all_encd_types'));
?>
登入後複製

Output:

PHP 輸出緩衝

Conclusion

PHP output buffering is an efficient way of giving an output to the end-user by keeping the data into a buffer before putting it to the browser it keeps the data on hold and then it assigns a variable to make the reference as it gives programmers the ability to change and manipulate it accordingly for the end-user with proper requirement.

以上是PHP 輸出緩衝的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!