Forum Moderators: coopster
Flushes the system write buffers of PHP and whatever backend PHP is using (CGI, a web server, etc). This attempts to push current output all the way to the browser with a few caveats.
What does that mean in layman's terms?
When is current output not pushed "all the way" to the browser?
<?php
$cacheFile = 'cache/' . md5($_SERVER['REQUEST_URI']) . '.txt';
if(file_exists($cacheFile)) {
echo $cacheFile;
die();
}
ob_start();
// Generate your output here, e.g. an HTML page
$buffer = ob_get_clean();
file_put_contents($cacheFile, $buffer);
echo $buffer;
?> When is current output not pushed "all the way" to the browser?
But do I ever need ob_flush() if I don't use ob_start() first?
echo <<<EOF
<html>
<head>
<!-- header stuff -->
</head>
EOF;
flush();
echo <<<EOF
<body>
<!-- navigation stuff -->
EOF;
flush();
echo <<<EOF
<!-- main stuff -->
EOF;
flush();
echo <<<EOF
<!-- footer stuff -->
EOF;
When is current output not pushed "all the way" to the browser?When you discover halfway through the process that you won’t be building the page after all. Years ago, I had a clutch of php pages that were made by
echo <<<EOF
<input class="btn bg-primary" type="submit" name="submit" value="Preview">
<input class="btn bg-primary" type="submit" value="Submit" name="submit"></form>
</div>
</div>
</div>
</div>
EOF;
flush();
echo <<<EOF
<div class="row">
<!-- yada yada yada -->
EOF;