【问题标题】:What is the difference when using Output Buffer on PHP?在 PHP 上使用输出缓冲区有什么区别?
【发布时间】:2014-12-10 04:00:22
【问题描述】:

我是 PHP 新手,有一个关于输出缓冲的问题。我在网上找到了这段代码:

ob_start();             
system('ipconfig /all');                
$contents = ob_get_contents();              
ob_end_clean();                     
$searchFor = "Physical";                
$pmac = strpos($contents, $searchFor);              
$mac = substr($contents, ($pmac + 36), 17);             
return $mac;

一切正常,但我不明白这里输出缓冲区的使用情况。如果我将其更改为:

$contents = system('ipconfig /all');                
$searchFor = "Physical";                
$pmac = strpos($contents, $searchFor);              
$mac = substr($contents, ($pmac + 36), 17);             
return $mac;

它似乎无法过滤$contents的内容来找到mac地址。那么输出缓冲对此有什么作用呢?

根据我对输出缓冲的了解,它将所有页面加载到一个变量中,然后一次全部返回,因此页面一次加载并且更快。我真的看不出这会如何改变输出在这种情况下如此剧烈。

【问题讨论】:

    标签: php macos output buffering


    【解决方案1】:

    system() 调用不仅会运行命令,还会显示输出。至少您的示例代码应该已经分配了返回值。 此外还需要输出缓冲,因为system 调用会将命令结果直接刷新到网络服务器(除非有明确的输出缓冲区)。

    这里应该使用的是exec() - 立即将结果分配给& $contents

    【讨论】:

    • 谢谢!所以在最基本的层面上,输出缓冲区在输出之前捕获 system() 的输出(这甚至是一个词吗?:P),然后将值写入字符串,然后过滤它?
    猜你喜欢
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 2014-09-14
    相关资源
    最近更新 更多