Skip to content

Instantly share code, notes, and snippets.

@nhuhoai
Last active January 18, 2021 10:23
Show Gist options
  • Save nhuhoai/a5d8845a407fb2282152d0a412409631 to your computer and use it in GitHub Desktop.
Save nhuhoai/a5d8845a407fb2282152d0a412409631 to your computer and use it in GitHub Desktop.
Show contents while PHP script is running
<?php
/**
* Show contents while PHP script is running
* I found someday this script with MIT license but I lost the reference.
* It's mean I am not the author of this script, I just adapted for my own usage.
*
* PHP Version 7.4
*
* @category Function
* @package Print2
* @author Nhu-Hoai Robert Vo <[email protected]>
* @copyright 2021 Nhu-Hoai Robert Vo
* @license https://spdx.org/licenses/MIT.html MIT License
* @version 0.1.0
* @link https://www.nhuvo.ch/
* @since 0.1.0
*/
/**
* Fush data and display contents
*
* @return void
*/
function doFlush() : void
{
if (!headers_sent()) {
// Disable gzip in PHP
ini_set("zlib.output_compression", 0);
// Force disable compression in a header
header("Content-Encoding: none");
}
// Fill-up 4 kB buffer, if you have a lot of conntents to display
// Please increase the buffer size
echo str_pad("", 4096);
// Flush all buffers
do {
$flushed = @ob_end_flush();
} while ($flushed);
@ob_flush();
flush();
}
/**
* Show your text
*
* @param mixed $contents Contents to print
*
* @return void
*/
function print2($contents) : void
{
print $contents;
doFlush();
}
<?php
/**
* Show contents while PHP script is running (example)
*
* PHP Version 7.4
*
* @category Function
* @package Print2
* @author Nhu-Hoai Robert Vo <[email protected]>
* @copyright 2021 Nhu-Hoai Robert Vo
* @license https://spdx.org/licenses/MIT.html MIT License
* @version 0.1.0
* @link https://www.nhuvo.ch/
* @since 0.1.0
*/
require_once __DIR__ . DIRECTORY_SEPARATOR . "print2.php";
$br = "<br />";
if (php_sapi_name() == "cli") {
$br = "\n";
}
print2("Waiting 5 seconds...{$br}");
sleep(1);
print2("4{$br}");
sleep(1);
print2("3{$br}");
sleep(1);
print2("2{$br}");
sleep(1);
print2("1{$br}");
sleep(1);
print2("Tadaaaa");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment