PHP ob_start() 函数

PHP ob_start() 函数

ob_start函数用于在PHP中创建一个输出缓冲区。因为我们已经知道PHP是一种解释型语言,即PHP中的任何程序都是逐步执行的,一条语句接着一条语句的执行,这使得处理速度相对较慢。

所以,PHP使用ob_start()函数来创建一个输出缓冲区,将生成的HTML存储在一个簇/缓冲区或字符串变量中,然后再发送到渲染,从而提高速度并减少执行时间。

为了执行输出缓冲区,我们必须使用ob_start()函数,该函数使用回调函数来处理缓冲区的内容。

该函数在成功传输时返回TRUE,在失败时返回FALSE。

语法:

ob_start (callback, chunk_size, flags);
序号 参数 描述 必填/可选
1 callback 回调的主要任务是在内容刷新之前处理所有缓冲区内容,即从输出缓冲区获取全部内容,并将返回的内容作为字符串发送到浏览器进行渲染。 此外,回调本身还包含两个参数(buffer,phase) 缓冲区: 输出缓冲区的内容 阶段: 使用的标记 可选
2 chunk_size 此参数用于设置输出缓冲区的大小,并在达到所需分块大小时自动执行输出 可选
3 flags 此参数使用位掩码来控制所有将在输出缓冲区上执行的操作。此参数用于限制对缓冲区的访问和授权。
默认缓冲区标记为 PHP _ OUTPUT _ HANDLER _ STDFLAGS: 该标记执行3个标记的工作:清除、刷新和移除缓冲区
PHP _ OUTPUT _ HANDLER _ CLEANABLE – 清除缓冲区,包含ob_clean()、ob_end_clean()和ob_get_clean()
PHP _ OUTPUT _ HANDLER _ FLUSHABLE – 刷新缓冲区,包含ob_flush()、ob_end_flush()和ob_get_flush()
PHP _ OUTPUT _ HANDLER _ REMOVABLE – 移除缓冲区,包含ob_end_clean()、ob_end_flush()和ob_get_flush()
可选

示例1

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=" width = device-width, initial-scale=1.0 " >
    <title> PHP type casting to integer </title>
</head>
<body>


<?php
ob_start();
echo "anythimg written inside these ob_function will not be send to the browser";
ob_end_clean();

echo "only the content outside the ob _ start () function will be send to browser and will be displayed";
?>

</body>
</html>

输出:

PHP ob_start() 函数

在这个程序中,我们使用了 OB_START() 函数来创建输出缓冲区, OB_END_CLEAN() 函数将被用于清除创建的缓冲区中的所有数据;因此,只有 B_START() 函数和 OB_END_CLEAN() 函数之外的文本会被显示出来。

示例2

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=" width = device-width, initial-scale=1.0 " >
    <title> PHP type casting to integer </title>
</head>
<body>
<?php
function callback(firstbuffer){
// This function will reture everything in CAPS.
return (strtoupper(firstbuffer));
}
ob_start("callback");
echo "as we know buffer is converted to a string variable there we can apply all the properties of string using ob _ start () function";
?>
</body>
</html>

结果:

PHP ob_start() 函数

在这个程序中,首先我们声明了一个名为 CALL BACK() 的函数,使用变量 $firstbuffer 。这个函数的属性是将其内部所有内容返回为大写形式。我们使用 OB_START() 函数创建一个输出缓冲区,参数为 call back ,以启动这个函数。由于调用函数, OB_START() 之后的所有文本都将以大写形式显示。

示例3

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=" width = device-width, initial-scale=1.0 " >
    <title> PHP type casting to integer </title>
</head>
<body>

<?php
echo "original text" . "<br>";
echo "Betty Botter bought a bit of butter. 'But,' she said, 'the butter's bitter. If I put it in my batter, it will make my batter bitter. But, a bit of better butter will make my batter better.' So, she bought a bit of butter, better than her bitter butter." . "<br>";
echo "altered text" . "<br>";

function callback(mybuffer)
{
return (str_replace("b", "* * * ", mybuffer));
}
ob_start("callback");
?>
<html>
<body>
<p>Betty Botter bought a bit of butter. "But," she said, "the butter's bitter. If I put it in my batter, it will make my batter bitter. But, a bit of better butter will make my batter better." So, she bought a bit of butter, better than her bitter butter.</p>
</body>
</html>
<?php
echo "<br>";
ob_end_flush();
?>


</body>
</html>

输出:

PHP ob_start() 函数

正如我们所知,缓冲区将数据存储在一个字符串变量中。我们可以使用所有字符串的属性。在这个程序中,首先我们声明了带有变量$mybuffer的 CALL BACK() 函数。这个函数的属性是用另一个声明的字符串替换。我们使用了 OB_START() 函数来创建一个输出缓冲区,参数为 call back 用于启动函数。由于调用函数,所有 OB_START() 之后的 将被转换为*

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程