Search code examples
phpoutput-buffering

Output Buffering dumps empty string, 2560 char long


I am dumping variables with:

ob_start(null, 4096);
define('SITERNDNUM', rand(10000000,99999999));
// other declarations and vars
Session::init();
// 
$output_ob = ob_get_contents();
var_dump($output_ob);

and all the output I get is:

string(2560) " "

I have tried using the flush function and other output ones with no avail.


Solution

  • The string is not empty. It is 2560 bytes long. var_dump() cannot display all characters and cancels the output for certain characters.

    For your understanding:

    $str = "\x00\x01\x02\n";
    var_dump($str);  //string(4) " " 
    

    The hexadecimal notation can help to make all bytes visible.

    function strhex($s){
      return $s != '' ? '\\x'.implode('\\x',str_split(bin2hex($s),2)) : '';
    }
    
    
    $str = "\x00\x01\x02\n";
    
    echo strhex($str);  //  \x00\x01\x02\x0a
    

    If you use this function instead of var_dump, all characters will be visible.