PHP 程式語言的 ob_start() 函數有助於在特定提到的腳本中的任何類型的回顯和任何 HTML 之前啟用特定輸出的緩衝。我們都知道PHP是解釋型Web開發程式語言之一,因此程式中的每一語句都會依序執行。因此,PHP 有助於將 HTML 分塊傳送到 Web 瀏覽器,從而有助於降低效能。借助輸出緩衝,產生的 HTML 將在最後一次 PHP 腳本執行後儲存在緩衝區中。為了克服這個問題,PHP 的 ob_start() 就應運而生了。
廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
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 作為輸出傳回。
PHP 程式語言的 ob_start() 有助於在 PHP 腳本中的某些 HTML 內容的任何類型的任何類型的回顯之前啟用輸出緩衝區/緩衝。 ob_start() 函數不專門接受任何參數,但它透過接受一些可選參數來運作。它們是:回調參數、區塊大小參數和標誌參數。此 ob_start() 僅適用於 PHP 4、PHP 5 和 PHP 7 版本。它只能透過打開輸出緩衝來起作用。
以下是 PHP ob_start() 的範例:
這是一個說明 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!!"; ?>
輸出:
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:
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:
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.
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 –
以上是PHP ob_start()的詳細內容。更多資訊請關注PHP中文網其他相關文章!