Test PHP headers with PHPUnit

The issue is that PHPUnit will print a header to the screen and at that point you can’t add more headers. The work around is to run the test in an isolated process. Here is an example <?php class FooTest extends PHPUnit_Framework_TestCase { /** * @runInSeparateProcess */ public function testBar() { header(‘Location : http://foo.com’); } … Read more

How to clear previously echoed items in PHP

<?php ob_start(); echo ‘a’; print ‘b’; // some statement that removes all printed/echoed items ob_end_clean(); echo ‘c’; // the final output is equal to ‘c’, not ‘abc’ ?> Output buffering functions The output buffering functions are also useful in hackery to coerce functions that only print to return strings, ie. <?php ob_start(); var_dump($myVar); $data = … Read more

What is output buffering in PHP?

Output Buffering for Web Developers, a Beginner’s Guide: Without output buffering (the default), your HTML is sent to the browser in pieces as PHP processes through your script. With output buffering, your HTML is stored in a variable and sent to the browser as one piece at the end of your script. Advantages of output … Read more

Why doesn’t print output show up immediately in the terminal when there is no newline at the end?

The issue By default, output from a Python program is buffered to improve performance. The terminal is a separate program from your code, and it is more efficient to store up text and communicate it all at once, rather than separately asking the terminal program to display each symbol. Since terminal programs are usually meant … Read more

PHP Flush/ob_flush not working

The idea here is to disable output buffering, not enable it. As its name says, output buffering will save the output to memory and display it at the end of the script, or when explicitly asked for it. That being said, you don’t have to flush explicitly for every output. Use the following, before displaying … Read more

PHP buffer ob_flush() vs. flush()

ob_flush sends an application-initiated buffer. There may be multiple nested ob_start()‘s in any PHP script. ob_flush passes the current content to the upper layer. PHP itself might (at its own discretion) buffer output. This depends on the back-end. But usually FastCGI has a socket buffer on its own. Therefore flush() needs to be invoked as … Read more

What’s the use of ob_start() in php?

Think of ob_start() as saying “Start remembering everything that would normally be outputted, but don’t quite do anything with it yet.” For example: ob_start(); echo(“Hello there!”); //would normally get printed to the screen/output to browser $output = ob_get_contents(); ob_end_clean(); There are two other functions you typically pair it with: ob_get_contents(), which basically gives you whatever … Read more

What exactly is file.flush() doing?

There’s typically two levels of buffering involved: Internal buffers Operating system buffers The internal buffers are buffers created by the runtime/library/language that you’re programming against and is meant to speed things up by avoiding system calls for every write. Instead, when you write to a file object, you write into its buffer, and whenever the … Read more

What is output buffering?

Output Buffering for Web Developers, a Beginner’s Guide: Without output buffering (the default), your HTML is sent to the browser in pieces as PHP processes through your script. With output buffering, your HTML is stored in a variable and sent to the browser as one piece at the end of your script. Advantages of output … Read more