略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Output Control 函数

2024-11-14

Output Control 函数

参见

See also header() and setcookie().

目录

  • flush — 刷新输出缓冲
  • ob_clean — 清空(擦掉)输出缓冲区
  • ob_end_clean — 清空(擦除)缓冲区并关闭输出缓冲
  • ob_end_flush — 冲刷出(送出)输出缓冲区内容并关闭缓冲
  • ob_flush — 冲刷出(送出)输出缓冲区中的内容
  • ob_get_clean — 得到当前缓冲区的内容并删除当前输出缓。
  • ob_get_contents — 返回输出缓冲区的内容
  • ob_get_flush — 刷出(送出)缓冲区内容,以字符串形式返回内容,并关闭输出缓冲区。
  • ob_get_length — 返回输出缓冲区内容的长度
  • ob_get_level — 返回输出缓冲机制的嵌套级别
  • ob_get_status — 得到所有输出缓冲区的状态
  • ob_gzhandler — 在ob_start中使用的用来压缩输出缓冲区中内容的回调函数。ob_start callback function to gzip output buffer
  • ob_implicit_flush — 打开/关闭绝对刷送
  • ob_list_handlers — 列出所有使用中的输出处理程序。
  • ob_start — 打开输出控制缓冲
  • output_add_rewrite_var — 添加URL重写器的值(Add URL rewriter values)
  • output_reset_rewrite_vars — 重设URL重写器的值(Reset URL rewriter values)
add a noteadd a note

User Contributed Notes 10 notes

up
18
jgeewax a t gmail
14 years ago
It seems that while using output buffering, an included file which calls die() before the output buffer is closed is flushed rather than cleaned. That is, ob_end_flush() is called by default.

<?php
// a.php (this file should never display anything)
ob_start();
include(
'b.php');
ob_end_clean();
?>

<?php
// b.php
print "b";
die();
?>

This ends up printing "b" rather than nothing as ob_end_flush() is called instead of ob_end_clean(). That is, die() flushes the buffer rather than cleans it. This took me a while to determine what was causing the flush, so I thought I'd share.
up
7
Anonymous
13 years ago
You possibly also want to end your benchmark after the output is flushed.

<?php
your_benchmark_start_function
();

ob_start ();
for (
$i = 0; $i < 5000; $i++)
    echo
str_repeat ("your string blablabla bla bla", (rand() % 4) + 1)."<br>\n";

                              <----------
echo
your_benchmark_end_function();      |
ob_end_flush (); ------------------------
?>
up
3
gruik at libertysurf dot fr
17 years ago
For those who are looking for optimization, try using buffered output.

I noticed that an output function call (i.e echo()) is somehow time expensive. When using buffered output, only one output function call is made and it seems to be much faster.
Try this :

<?php
your_benchmark_start_function
();

for (
$i = 0; $i < 5000; $i++)
    echo
str_repeat ("your string blablabla bla bla", (rand() % 4) + 1)."<br>\n";

echo
your_benchmark_end_function();
?>

And then :

<?php
your_benchmark_start_function
();

ob_start ();
for (
$i = 0; $i < 5000; $i++)
    echo
str_repeat ("your string blablabla bla bla", (rand() % 4) + 1)."<br>\n";

echo
your_benchmark_end_function();
ob_end_flush ();
?>
up
1
basicartsstudios at hotmail dot com
15 years ago
Sometimes you might not want to include a php-file under the specifications defined in the functions include() or require(), but you might want to have in return the string that the script in the file "echoes".

Include() and require() both directly put out the evaluated code.

For avoiding this, try output-buffering:
<?php
ob_start
();
eval(
file_get_contents($file));
$result = ob_get_contents();
ob_end_clean();
?>
or
<?php
ob_start
();
include(
$file);
$result = ob_get_contents();
ob_end_clean();
?>
which i consider the same, correct me if I'm wrong.

Best regards, BasicArtsStudios
up
0
Attila Houtkooper
7 years ago
Take care to take exceptions in the code in mind when using ob_start and ob_get_contents. If you do not do this, the number of calls to ob_start will not match those to ob_end and you're not gonna have a good time.

<?php
public function requireIntoVariable($path) {
   
ob_start();

    try {
        require
$path;
    } catch (
Exception $e) {
       
ob_end_clean();
        throw
$e;
    }

   
$output = ob_get_contents();
   
ob_end_clean();
    return
$output;
}
?>
up
-1
kend52 at verizon dot net
16 years ago
I ran out of memory, while output buffering and drawing text on imported images. Only the top portion of the 5MP image was displayed by the browser.  Try increasing the memory limit in either the php.ini file( memory_limit = 16M; ) or in the .htaccess file( php_value memory_limit "16M" ). Also see function memory_get_usage() .
up
-2
della at sun dot com
13 years ago
Sometimes users are blaming about slow pages ... not being aware that mostly this is due to network issues.
So I've decided to add some statistics at the end of my pages:

At beginning I start the counters:

<?php
 
function microtime_float() {
    if (
version_compare(PHP_VERSION, '5.0.0', '>'))  return microtime(true);
    list(
$u,$s)=explode(' ',microtime()); return ((float)$u+(float)$s); 
  }
 
$initime=microtime_float();
 
ob_start();
 
ob_implicit_flush();
?>

And at the end I show the statistics:

<?php
 
echo "PHP Time: ".round((microtime_float()-$initime)*1000)." msecs. ";
  echo
"Size: ".round_byte(strlen(ob_get_contents()));
 
ob_end_flush();
?>

(round_byte is my function to print byte sizes)
up
-2
kamermans at teratechnologies dot net
15 years ago
Output buffering is set to '4096' instead of 'Off' or '0' by default in the php-5.0.4-10.5 RPM for Fedora Core release 4 (Stentz).  This has cost me much time!
up
-7
trucex [um, at] gmail [um, dot] com
15 years ago
Unfortunately, the PHP guys didn't build support into any of the image output functions to return the image instead of outputting it.

Fortunately, we have output buffering to fix that.

<?php

$im
= imagecreatetruecolor(200, 200);

// Other image functions here...

ob_start();
imagepng($im);
$imageData = ob_get_contents();
ob_clean();

?>

You can now use the $imageData variable to either create another GD image, save it, put it in a database, make modifications to the binary, or output it to the user. You can easily check the size of it as well without having to access the disk...just use strlen();
up
-13
webmaster at wistex dot com
15 years ago
Now this just blew my mind. I had a problem with MySQL being incredibly slow on Windows 2003 running IIS... on ASP/VBScript pages. PHP is also installed on the server and so is Microsoft SQL 2005 Express. (Yes, we're running ASP, PHP, MySQL and MS SQL on the same Windows 2003 Server using IIS.)

I was browsing the internet for a solution and saw a suggestion that I change output_buffering to on if MySQL was slow for PHP pages.  Since we also served PHP pages with MySQL from the same server, it caught my eye.  For the hell of it, I went into php.ini and changed output_buffering to on and suddenly MySQL and ASP was faster... MySQL and PHP was faster... Microsoft SQL Server 2005 Express and ASP was faster.... everything was faster... even stuff that had no PHP!

And I didn't even have to restart IIS. As soon as I saved the php.ini file with the change, everything got faster.

Apparently PHP and MySQL and IIS are so intertwined somehow that changing the buffering setting really effects the performance of the entire server.

So, if you are having performance problems on Windows 2003 & IIS, you might try setting output_buffering = On in php.ini if you happen to have PHP installed.  Having it set to off apparently effects the performance of Windows 2003 and IIS severely... even for webpages that do not use PHP or MySQL.

官方地址:https://www.php.net/manual/en/ref.outcontrol.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3