PHP ob_start()
PHP 程式語言的 ob_start() 函數有助於在特定提到的腳本中的任何類型的回顯和任何 HTML 之前啟用特定輸出的緩衝。我們都知道PHP是解釋型Web開發程式語言之一,因此程式中的每一語句都會依序執行。因此,PHP 有助於將 HTML 分塊傳送到 Web 瀏覽器,從而有助於降低效能。借助輸出緩衝,產生的 HTML 將在最後一次 PHP 腳本執行後儲存在緩衝區中。為了克服這個問題,PHP 的 ob_start() 就應運而生了。
廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
PHP ob_start() 的語法和參數
ob_start();
PHP 程式語言的 ob_start() 函數接受許多可選參數。他們是:
- 回呼函數
 - 塊大小
 - 旗幟。
 
1。回呼函數:回呼函數參數有助於期望一個函數,該函數通常會取得輸出緩衝區的內容,然後傳回一個專門傳送到瀏覽器進行渲染的字串。回調函數參數通常用於壓縮 HTML 內容。它是 ob_start() 函數的可選參數。
2。區塊大小:ob_start() 函數的區塊大小參數也是另一個可選參數,有助於設定輸出緩衝區大小,然後在緩衝區超出或已滿時輸出。
3。 Flags:PHP 程式語言的 ob_start() 函數的 flags 參數有助於接受位元掩碼,以控制將在某些特定輸出緩衝區上實現的某些操作。 flags 參數有助於傳遞受限存取權限,預設權限有助於授予清理和緩衝區刪除的存取權限。這個參數和其他兩個參數一樣也是一個可選參數。
PHP ob_start() 函數的回傳類型:
ob_start() 函數有助於在成功輸出時傳回 TRUE 值,否則您將得到 False 作為輸出傳回。
ob_start() 在 PHP 中如何運作?
PHP 程式語言的 ob_start() 有助於在 PHP 腳本中的某些 HTML 內容的任何類型的任何類型的回顯之前啟用輸出緩衝區/緩衝。 ob_start() 函數不專門接受任何參數,但它透過接受一些可選參數來運作。它們是:回調參數、區塊大小參數和標誌參數。此 ob_start() 僅適用於 PHP 4、PHP 5 和 PHP 7 版本。它只能透過打開輸出緩衝來起作用。
實作 PHP ob_start() 函數的範例
以下是 PHP ob_start() 的範例:
範例#1
這是一個說明 PHP 程式語言的 ob_start() 函數的範例,以便了解 ob_start() 函數的回呼功能。這裡先開啟 PHP 標籤,然後建立一個帶有參數的函數。然後函數內部的 return 函數與 strtoupper() 函數一起使用,以大寫字母傳回輸出。然後 ob_start() 函數與回調參數一起使用,這有助於更改輸出。這裡的輸出是在 echo 語句的幫助下提到的一個字串。這裡的字串是“Hello Educba!!”這將更改為大寫,例如“HELLO EDUCBA!!”。查看輸出,以便您了解語法中發生的情況。
代碼:
<?php
// This is PHP code which helps in illustrating the working
// of the ob_start() Function of PHP Language
function callback($buffer1){
// This function Returns Everything of output in CAPS.
return (strtoupper($buffer1));
}
ob_start("callback");
echo "Hello Educba!!";
?>輸出:

Example #2
This is also an example of illustrating the ob_start() function of the PHP Programming Language which helps in handling the output buffering. Here at first, inside of the PHP tags, a function called callback is created with the buffer1 as a parameter. Inside of the function str_replace() function is used which helps in returning the output of the output text just by replacing the required string text according to the need. Here mangoes and Pomegranates and the mangoes text will be replaced by the “Pomegranates” text. Then the function parenthesis are closed. Then ob_start() function is used with the callback parameter for the required return output. Then HTML tags are used. Inside the HTML and BODY tags, some string text is used. The string text can be a string or some paragraph that is actually mentioned based on our requirement. Her in the following text, the string text “mangoes” will be replaced with “Pomegranates”.
Code:
<?php
function callback($buffer1)
{
// This will help in replacing all Mangoes with the Pomegranates
return (str_replace("mangoes", "Pomegranates", $buffer1));
}
ob_start("callback");
?>
<html>
<body>
<p>It's like comparing the mangoes to Pomegranates.</p>
</body>
</html>
<?php
echo "<br>";
ob_end_flush();
?>Output:

Example #3
This is an example of illustrating the ob_start() of the PHP Programming Language. Here at the first inside of the PHP tags, If the condition is created and then inside of it a function is mentioned as the condition and then the ob_start() function is used along with the callback parameter, chunk size parameter along with the flag parameters. If the IF condition is TRUE then the “Your PHP version is greater than or equal to PHP 5.4.0 version“ string text will be printed and if the IF condition is FALSE value then else condition statements will be printed. In ELSE also we used the ob_start() function is used with callback value as NULL, Chunk size parameter value as 0 and FALSE as the FLAG value. So this doesn’t produce any output. So to recognize this we used some string text with ECHO is used. PHP_OUTPUT_HANDLER_REMOVABLE is used to remove the output which is created by ob_start() just before the end of the script. PHP_OUTPUT_HANDLER_STDFLAG is the default set of some output buffer flags and it is equivalent to PHP_OUTPUT_HANDLER_REMOVABLE.
Code:
<?php
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
ob_start(null, 0, PHP_OUTPUT_HANDLER_STDFLAGS ^
PHP_OUTPUT_HANDLER_REMOVABLE);
echo "Your PHP version is greater than or equal to PHP 5.4.0 version";
} else {
ob_start(null, 0, false);
echo "Your PHP Version is less than PHP 5.4.0 version";
}
?>Output:

Conclusion
I hope you learned what is the definition of ob_start of the PHP Programming Language along with its syntax and explanations, How the ob_start() function works in PHP along with various examples of ob_start() function to understand the ob_start() better and so easily.
Recommended Article
This is a guide to the PHP ob_start(). Here we discuss the introduction, syntax, and working of the ob_start() function in PHP along with different examples and code implementation. You can also go through our other suggested articles to learn more –
- Overview of Abstract Class in Python
 - What is Abstract Class in PHP?
 - Socket Programming in PHP with Methods
 - PHP chop() | How to Work?
 
以上是PHP ob_start()的詳細內容。更多資訊請關注PHP中文網其他相關文章!
									熱AI工具
											
											Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片
											
											AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。
											
											Undress AI Tool
免費脫衣圖片
											
											Clothoff.io
AI脫衣器
											
											Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!
								熱門文章
									熱工具
											
											記事本++7.3.1
好用且免費的程式碼編輯器
											
											SublimeText3漢化版
中文版,非常好用
											
											禪工作室 13.0.1
強大的PHP整合開發環境
											
											Dreamweaver CS6
視覺化網頁開發工具
											
											SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)
								熱門話題
								
								適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南
								Dec 24, 2024 pm	 04:42 PM
								PHP 8.4 帶來了多項新功能、安全性改進和效能改進,同時棄用和刪除了大量功能。 本指南介紹如何在 Ubuntu、Debian 或其衍生版本上安裝 PHP 8.4 或升級到 PHP 8.4
								
								如何設定 Visual Studio Code (VS Code) 進行 PHP 開發
								Dec 20, 2024 am	 11:31 AM
								Visual Studio Code,也稱為 VS Code,是一個免費的原始碼編輯器 - 或整合開發環境 (IDE) - 可用於所有主要作業系統。 VS Code 擁有大量針對多種程式語言的擴展,可以輕鬆編寫
								
								在PHP API中說明JSON Web令牌(JWT)及其用例。
								Apr 05, 2025 am	 12:04 AM
								JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、
								
								您如何在PHP中解析和處理HTML/XML?
								Feb 07, 2025 am	 11:57 AM
								本教程演示瞭如何使用PHP有效地處理XML文檔。 XML(可擴展的標記語言)是一種用於人類可讀性和機器解析的多功能文本標記語言。它通常用於數據存儲
								
								php程序在字符串中計數元音
								Feb 07, 2025 pm	 12:12 PM
								字符串是由字符組成的序列,包括字母、數字和符號。本教程將學習如何使用不同的方法在PHP中計算給定字符串中元音的數量。英語中的元音是a、e、i、o、u,它們可以是大寫或小寫。 什麼是元音? 元音是代表特定語音的字母字符。英語中共有五個元音,包括大寫和小寫: a, e, i, o, u 示例 1 輸入:字符串 = "Tutorialspoint" 輸出:6 解釋 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。總共有 6 個元
								
								解釋PHP中的晚期靜態綁定(靜態::)。
								Apr 03, 2025 am	 12:04 AM
								靜態綁定(static::)在PHP中實現晚期靜態綁定(LSB),允許在靜態上下文中引用調用類而非定義類。 1)解析過程在運行時進行,2)在繼承關係中向上查找調用類,3)可能帶來性能開銷。
								
								什麼是PHP魔術方法(__ -construct,__destruct,__call,__get,__ set等)並提供用例?
								Apr 03, 2025 am	 12:03 AM
								PHP的魔法方法有哪些? PHP的魔法方法包括:1.\_\_construct,用於初始化對象;2.\_\_destruct,用於清理資源;3.\_\_call,處理不存在的方法調用;4.\_\_get,實現動態屬性訪問;5.\_\_set,實現動態屬性設置。這些方法在特定情況下自動調用,提升代碼的靈活性和效率。
							
											
                
												
											
											