首页 > 后端开发 > php教程 > PHP ob_start()

PHP ob_start()

PHPz
发布: 2024-08-29 12:50:04
原创
704 人浏览过

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. 回调函数
  2. 块大小
  3. 旗帜。

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!!";
?>
登录后复制

输出:

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中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板