ob_get_contents 在 PHP 中是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14671961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does ob_get_contents work in Php?
提问by Koray Tugay
This is a sample code from the book I am reading:
这是我正在阅读的书中的示例代码:
ob_start();
include("{$path}.ini");
$string = ob_get_contents();
ob_end_clean();
$pairs = parse_ini_string($string);
My question is, how does ob_get_contents()know what to get contents from? ({$path}.ini in this situation)?
我的问题是,如何ob_get_contents()知道从哪里获取内容?({$path}.ini 在这种情况下)?
采纳答案by Rudi Visser
ob_get_contentssimply gets the contents of the output buffer since you called ob_start(). Essentially, an output buffer in PHP catches anything that would have been outputto the browser (excluding headers). It's useful in cases where you may need to filter some output, or you're using a PHP method (such as var_dump) that writes output directly to screen, and you would instead like the return value of the method in a string.
ob_get_contents简单地获取输出缓冲区的内容,因为你调用了ob_start(). 从本质上讲,PHP 中的输出缓冲区会捕获本应输出到浏览器的任何内容(不包括标题)。如果您可能需要过滤某些输出,或者您正在使用var_dump将输出直接写入屏幕的 PHP 方法(例如),而您希望以字符串形式提供方法的返回值,这将非常有用。
In this case, because you're include()ing the .inifile, it's contents will be essentially output to screen, and ob_get_contents()will get the content of the file.
在这种情况下,因为您正在include()处理.ini文件,所以它的内容基本上会输出到屏幕上,并ob_get_contents()会获得文件的内容。
If you were to put echo "I'm a little teapot short and stout";underneath the include, this would also be included in $stringafter the body of the .inifile.
如果您要放在echo "I'm a little teapot short and stout";之下include,这也将包含在文件$string正文之后.ini。
In your specific case, however, output buffering is an unnecessary overhead, simply use file_get_contentson the .inifile. I'm not sure why a book would even have this code in it at all.
但是,在您的特定情况下,输出缓冲是不必要的开销,只需file_get_contents在.ini文件上使用即可。我不知道为什么一本书甚至会在其中包含此代码。
回答by leftclickben
The "ob" stands for "output buffer". When you call ob_start(), PHP reroutes all output (using echo, etc) to the output buffer. Then you can use the other ob_*functions to retrieve and/or clear the buffer contents.
“ob”代表“输出缓冲区”。当您调用 时ob_start(),PHP 会将所有输出(使用echo等)重新路由到输出缓冲区。然后您可以使用其他ob_*函数来检索和/或清除缓冲区内容。
In your example, it will buffer any output generated by the file referenced by "{$path}.ini". When you include it, its output is added to the buffer, and when you call ob_get_contents(), it retrieves the contents of the buffer.
在您的示例中,它将缓冲由"{$path}.ini". 当你包含它时,它的输出被添加到缓冲区中,当你调用时ob_get_contents(),它会检索缓冲区的内容。
回答by leftclickben
From PHP:
来自 PHP:
ob_start — Turn on output buffering
ob_get_contents — Return the contents of the output buffer
ob_end_clean — Clean (erase) the output buffer and turn off output buffering
Now, ob_get_contents can collect all buffer that outputted.
现在,ob_get_contents 可以收集所有输出的缓冲区。
回答by Nemanja Boric
ob_get_contents()is getting everything that is echoed after calling ob_start()function, so there is not anything special about {$path}.ini- you are required to echo data you want to collect (yes, even outputs of simple echoor print_rcalls will be collected - sometimes useful for debugging simple scripts).
ob_get_contents()正在获取调用ob_start()函数后回显的所有内容,因此没有什么特别之处{$path}.ini-您需要回显要收集的数据(是的,甚至会收集简单echo或print_r调用的输出-有时对调试简单脚本很有用)。
You may understand ob_start()function as a simple redirection from screen to (invisible) PHP internal buffer which is later read by ob_get_contents(). So you will be able to redirect anything that you may see on the screen without calling ob_start()function (even the whole web pages).
您可以将ob_start()函数理解为从屏幕到(不可见的)PHP 内部缓冲区的简单重定向,稍后由ob_get_contents(). 因此,您将能够在不调用ob_start()函数的情况下重定向您可能在屏幕上看到的任何内容(甚至整个网页)。

