3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Fiddle in response to StackOverflow question: https://stackoverflow.com/questions/69845122 // by HappyDog // According to the PHP documentation, returning false from an output-buffer // callback should result in the same result as returning the original input, // but this fiddle shows that returning false actually breaks the buffer if it // is 'cleaned'. The solution is to always return a string value. ////////////////////////////// // CALLBACKS // Returns false, which means 'use the buffer as supplied'. function ReturnFalse($Buffer, $Phase) { global $arrCallbacks; $arrCallbacks[] = array($Buffer, $Phase); return false; } // Returns the supplied buffer - result should be identical to returning false. function ReturnBuffer($Buffer, $Phase) { global $arrCallbacks; $arrCallbacks[] = array($Buffer, $Phase); return $Buffer; } ////////////////////////////// // TESTING CODE foreach (array("ReturnFalse", "ReturnBuffer") as $Callback) { $arrCallbacks = array(); print("== Testing " . $Callback . " ===\n"); ob_start($Callback); print("First\n"); ob_clean(); print("Second\n"); ob_end_flush(); print("\n"); var_dump($arrCallbacks); }
Output for 5.4.10 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
== Testing ReturnFalse === Second array(1) { [0]=> array(2) { [0]=> string(6) "First " [1]=> int(3) } } == Testing ReturnBuffer === Second array(2) { [0]=> array(2) { [0]=> string(6) "First " [1]=> int(3) } [1]=> array(2) { [0]=> string(7) "Second " [1]=> int(8) } }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 == Testing ReturnFalse === Second array(1) { [0]=> array(2) { [0]=> string(6) "First " [1]=> int(3) } } == Testing ReturnBuffer === Second array(2) { [0]=> array(2) { [0]=> string(6) "First " [1]=> int(3) } [1]=> array(2) { [0]=> string(7) "Second " [1]=> int(8) } }
Output for 5.4.0 - 5.4.9
== Testing ReturnFalse === Second array(1) { [0]=> array(2) { [0]=> string(0) "" [1]=> int(3) } } == Testing ReturnBuffer === Second array(2) { [0]=> array(2) { [0]=> string(0) "" [1]=> int(3) } [1]=> array(2) { [0]=> string(7) "Second " [1]=> int(8) } }
Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
== Testing ReturnFalse === Second array(2) { [0]=> array(2) { [0]=> string(6) "First " [1]=> int(3) } [1]=> array(2) { [0]=> string(7) "Second " [1]=> int(4) } } == Testing ReturnBuffer === Second array(2) { [0]=> array(2) { [0]=> string(6) "First " [1]=> int(3) } [1]=> array(2) { [0]=> string(7) "Second " [1]=> int(4) } }

preferences:
312.28 ms | 403 KiB | 468 Q