PHP 输出缓冲是一个确认 PHP 引擎保存数据的过程,同时在输入进行处理时提供输出。一旦 PHP 引擎获得处理后的数据并执行以提供输出,那么同时该数据就会被一点一点地发送到引擎并发送到浏览器。如果使用上述的输出缓冲机制来执行,那么这将在数据处理方面提供更高的效率和可行性,因为数据首先存储在变量中,然后作为脚本的一部分发送到浏览器。
广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法:
输出缓冲没有固定的格式,但可以通过以下方式表示和使用:
<?php function to start php_info( ); // processing before giving the output. use variable to assign the final value as an output ?>
PHP 中的输出缓冲在其工作方面具有重要意义,如下所示:
让我们讨论 PHP 输出缓冲的示例。
此程序演示了由用户定义的回调() 函数,该函数将替换变量中定义的值,如输出所示。
代码:
<!DOCTYPE html> <html> <body> <?php function cll_bck($buff) { return (str_replace("Mobile", "Tabs", $buff)); } ob_start("cll_bck"); ?> <html> <body> <p>Everyone_prefers_Mobile_over_Tabs.</p> </body> </html> <?php ob_end_flush(); ?> </body> </html>
输出:
此程序演示了 ob_get_contents() 函数,用于获取定义到最终引擎的内容,同时传递输出中所示的变量。
代码:
<!DOCTYPE html> <html> <body> <?php ob_start(); echo "Today_day_is_good. "; $o_t_1 = ob_get_contents(); echo "and_pleasant"; $o_t_2 = ob_get_contents(); ob_end_clean(); var_dump($o_t_1, $o_t_2); ?> </body> </html>
输出:
This program demonstrates the ob_start function where the output buffering gets initiated and then it gets displayed as shown in the output.
Code:
<!DOCTYPE html> <html> <body> <?php ob_start(); echo 'Text written will_get displayed easily.'; ?> </body> </html>
Output:
This program demonstrates the use of text that will get removed once the ob_end_clean function is called as shown in the output.
Code:
<!DOCTYPE html> <html> <body> <?php ob_start(); echo 'Text_written_will_get_removed_easily_using ob_end_clean.'; ob_end_clean(); ?> </body> </html>
Output:
This program demonstrates the ob_list_handlers() function which is used to return an array with the output buffer handler with the list of handlers as shown in the output.
Code:
<!DOCTYPE html> <html> <body> <?php print_r(ob_list_handlers()); ob_end_flush(); ob_start("ob_gz_handler"); print_r(ob_list_handlers()); ob_end_flush(); ob_start(function($str_2) { return $str_2; }); print_r(ob_list_handlers()); ob_end_flush(); ?> </body> </html>
Output:
This program demonstrates the encoding and decoding of all types of possible codes being defined but if in case something is missing, or the browser is getting some value as wrong then it will return the output as shown.
Code:
<!DOCTYPE html> <html> <body> <pre class="brush:php;toolbar:false"> <?php iconv_set_encoding("int_encd", "internal_UTF_8"); iconv_set_encoding("o/p_encd", "ISO-8859-1"); var_dump(iconv_get_encoding('all_encd_types')); ?>