Функции контроля вывода

Смотрите также

Смотрите также header() и 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_implicit_flush — Включает/выключает неявный сброс
  • ob_list_handlers — Возвращает список активных обработчиков вывода
  • ob_start — Включает буферизацию вывода
  • output_add_rewrite_var — Добавляет значения в обработчик перезаписи URL
  • output_reset_rewrite_vars — Сбросить значения обработчика URL
Добавить

Примечания пользователей 8 notes

up
17
jgeewax a t gmail
17 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
6
Anonymous
15 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
20 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
Attila Houtkooper
9 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
-2
basicartsstudios at hotmail dot com
17 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
-3
kend52 at verizon dot net
19 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
kamermans at teratechnologies dot net
18 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
-5
della at sun dot com
15 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)
To Top