Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ob_start not executing callback

Tags:

php

ob-start

I'm having issues with ob_start. Not sure what the deal is, but I've bubbled it down to the simplest possible test case... still to no avail. I would expect this code to output 'bar' to the stdout, but I'm getting nothing back, and no errors in my error log.

<?php
function gzhandler_ex($buffer, $mode)
{
    echo 'bar';
}

ob_start('gzhandler_ex');
echo 'foo';
ob_flush(); 

I've never seen this before, but I don't typically use callbacks like this.

like image 253
John Green Avatar asked May 15 '11 10:05

John Green


People also ask

What does Ob_start () function do?

The ob_start() function creates an output buffer. A callback function can be passed in to do processing on the contents of the buffer before it gets flushed from the buffer. Flags can be used to permit or restrict what the buffer is able to do.

What is Ob_start () and Ob_end_flush () in PHP?

While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. The contents of this internal buffer may be copied into a string variable using ob_get_contents(). To output what is stored in the internal buffer, use ob_end_flush().

Why do we use Ob_start in PHP?

Using ob_start allows you to keep the content in a server-side buffer until you are ready to display it. This is commonly used to so that pages can send headers 'after' they've 'sent' some content already (ie, deciding to redirect half way through rendering a page).

What is use of Ob_end_clean in PHP?

The ob_end_clean() function deletes the topmost output buffer and all of its contents without sending anything to the browser.


1 Answers

Your handler function should return the content you want to output, not echo it.

function gzhandler_ex($buffer, $mode)
{
    return 'bar';
}

Also, the ob_flush() is unnecessary when called at the end of the script; it is implicit.

like image 180
lonesomeday Avatar answered Sep 22 '22 07:09

lonesomeday