PHP ob_start()

PHPz
リリース: 2024-08-29 12:50:04
オリジナル
567 人が閲覧しました

PHP プログラミング言語の ob_start() 関数は、前述の特定のスクリプト内のあらゆるタイプのエコーや HTML の前に、特定の出力のバッファリングを有効にするのに役立ちます。 PHP は解釈型 Web 開発プログラミング言語の 1 つであるため、プログラム内のすべてのステートメントが次々に実行されることは誰もが知っています。したがって、PHP は HTML をいくつかのチャンクで Web ブラウザに送信するのに役立ち、パフォーマンスの低下に役立ちます。出力バッファリングの助けを借りて、生成された HTML は、最後の PHP スクリプトの実行後にバッファーに保存されます。これを克服するために、PHP の ob_start() が登場しました。

広告 このカテゴリーの人気コース PHP 開発者 - 専門分野 | 8コースシリーズ | 3 つの模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

PHP ob_start() の構文とパラメータ

ob_start();
ログイン後にコピー

PHP プログラミング言語の ob_start() 関数は、多数のオプションのパラメーターを受け入れます。それらは次のとおりです:

  1. コールバック関数
  2. チャンクサイズ
  3. フラグ。

1.コールバック関数: コールバック関数パラメーターは、通常、出力バッファーのコンテンツを取得し、レンダリングのためにブラウザーに特別に送信される文字列を返す関数を予期するのに役立ちます。通常、HTML コンテンツを圧縮するために使用されるコールバック関数のパラメーター。 ob_start() 関数のオプションのパラメータです。

2.チャンク サイズ: ob_start() 関数のチャンク サイズ パラメーターも別のオプションのパラメーターであり、出力バッファー サイズの設定に役立ち、バッファーが超過またはいっぱいの場合に出力されます。

3.フラグ: PHP プログラミング言語の ob_start() 関数の flags パラメータは、特定の出力バッファに実装されるいくつかの操作を制御するためにビットマスクを受け入れるのに役立ちます。 flags パラメータは制限されたアクセスを渡すのに役立ち、デフォルトのアクセス許可はクリーンおよびバッファ削除へのアクセスを与えるのに役立ちます。このパラメータも、他の 2 つのパラメータと同様にオプションのパラメータです。

PHP の ob_start() 関数の戻り値の型:

ob_start() 関数は、成功の出力で TRUE 値を返すのに役立ちます。そうでない場合は、出力の戻り値として False が返されます。

PHP では ob_start() はどのように動作しますか?

PHP プログラミング言語の ob_start() は、PHP スクリプト内の一部の HTML コンテンツのあらゆる種類のエコーの前に、出力バッファ/バッファリングを有効にするのに役立ちます。 ob_start() 関数は特にパラメーターを受け入れませんが、いくつかのオプションのパラメーターを受け入れることで機能します。それらは、コールバック パラメータ、チャンク サイズ パラメータ、およびフラグ パラメータです。この ob_start() は、PHP 4、PHP 5、および PHP 7 バージョンでのみ動作します。出力バッファリングをオンにするだけで機能します。

PHP ob_start() 関数の実装例

以下は PHP ob_start() の例です:

例 #1

これは、ob_start() 関数のコールバック機能を理解するために、PHP プログラミング言語の 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!!";
?>
ログイン後にコピー

出力:

PHP ob_start()

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:

PHP ob_start()

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:

PHP ob_start()

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 –

  1. Overview of Abstract Class in Python
  2. What is Abstract Class in PHP?
  3. Socket Programming in PHP with Methods
  4. PHP chop() | How to Work?

以上がPHP ob_start()の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
php
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!