Search code examples
phperror-handlingbuffering

PHP buffering error <unknown> in line 0


Would anybody be kind to help me with any possible information on this error? causes? login? where to look/start?

 Cannot use output buffering in output buffering display handlers in <**Unknown**> on line **0**

Example code:

function test($b) { return _strrev($b); } 

ob_start("test"); 
echo "123"; 
ob_end_flush();

Information:

  • This is CentOS 6.7
  • This is PHP PHP 5.5.32 (cli)
  • This is NOT Wordpress
  • This is a simple PHP file with pseudo code (above)
  • No caching, no Varnish, no Nginx, no custom headers

Thanks


Solution

  • In an effort to make this question of some use to others I will summarise the comments section in the form of an answer.

    It turns out that if you make a coding error in the callback function used by ob_start it generates this somewhat unhelpful error message

    In testing I quite accidentally made a silly error while amending the callback funtion shown by the questioner, I coded

    <?php
    function test($b) {
        return str-replace('2', 'XXX', $b);
    }
    
    ob_start("test");
    echo "123";
    ob_end_flush();
    

    Note the misspelling of the str_replace() function.

    This generated the error

    Fatal error: Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0

    So it appears that PHP cannot report runtime errors in an ob_start callback funtion. Instead it must default to a generic error along the lines of Ouch that hurt

    Maybe one of us should report this as a PHP Bug

    After some more testing, this error senario produces a meaningful error in PHP 7.0.5

    Notice: Use of undefined constant str - assumed 'str' in tst.php on line 3

    Fatal error: Uncaught Error: Call to undefined function replace() in tst.php:3

    so this may be fixed now, but all previous PHP versions i.e. 5.4 / 5.5 / 5.6 produce the unhelpful error message