php ob_start的作用,输出缓冲 - 在php中使用ob_start()有什么用?

这是为了进一步澄清JD Isaaks的回答......

您经常遇到的问题是您使用php从许多不同的PHP源输出html,并且这些源通常无论出于何种原因通过不同的方式输出。

有时你想要直接输出到浏览器的文字html内容; 其他时候输出是动态创建的(服务器端)。

动态内容总是(?)将成为一个字符串。 现在你必须将这个字符串化的动态html与任何文字的,直接显示的html ...组合成一个有意义的html节点结构。

这通常会迫使开发人员将所有直接显示内容包装成一个字符串(正如JD Isaak所讨论的那样),这样它就可以与动态html一起正确地传递/插入......即使你不是真的 希望它包裹起来。

但是通过使用ob _ ##方法,你可以避免那个字符串包装混乱。 相反,文字内容输出到缓冲区。 然后在一个简单的步骤中,缓冲区的所有内容(所有文字html)将连接到您的dynamic-html字符串中。

(我的例子显示了将文字html输出到缓冲区,然后将其添加到html-string中......另请参阅JD Isaaks示例以查看字符串包装的html)。

//---------------------------------

$lvs_html = "" ;

$lvs_html .= "

html
" ;

$lvs_html .= gf_component_assembler__without_ob( ) ;

$lvs_html .= "

more html
" ;

$lvs_html .= "----
" ;

$lvs_html .= "

html
" ;

$lvs_html .= gf_component_assembler__with_ob( ) ;

$lvs_html .= "

more html
" ;

echo $lvs_html ;

// 02 - component contents

// html

// 01 - component header

// 03 - component footer

// more html

// ----

// html

// 01 - component header

// 02 - component contents

// 03 - component footer

// more html

//---------------------------------

function gf_component_assembler__without_ob( )

{

$lvs_html = "

01 - component header
" ; //

include( "component_contents.php" ) ;

$lvs_html .= "

03 - component footer
" ; // " ;

return $lvs_html ;

} ;

//---------------------------------

function gf_component_assembler__with_ob( )

{

$lvs_html = "

01 - component header
" ; //

ob_start();

include( "component_contents.php" ) ;

$lvs_html .= ob_get_clean();

$lvs_html .= "

03 - component footer
" ; // " ;

return $lvs_html ;

} ;

//---------------------------------

?>

02 - component contents

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值