I am absolutely stumped here. I must either be missing something very simple or dont understand how this works.
Output buffering does not work at all on MAMP PRO, all contents are simply being displayed on the page and nothing goes to the buffer, not even hello world. I have tried every example
I am creating a simple framework and output buffering just does not work.
I have a module class with a function that includes a file and the code simply shows on the page without me even clearing the buffer.
I have checked the php.ini file in both loaded configuration file and the configuration file shows output_buffering = 4096. I am so confused
Here is the code example:
//index.php
var_dump(ob_start());//returns true
echo "Hello World"; //prints straight to the screen
include MODULES.'/home.php'; //output comes straight out
var_dump(ob_get_contents());//Shows html string
$test = ob_get_contents();
echo $test; //Output gets displayed twice
In PHP.ini: output_buffering=4096;
ob_get_contents
does not clear the buffer so when a script ends it is flushed to the output as usual.
If you only want to get data as string and clear buffer use ob_get_clean()
You can think of output buffering as creating "bookmarks" or "restore points" in output buffer. For example:
echo 'hello'
- output buffer has "hello" stringob_start()
- "bookmark" is createdecho 'world'
- output buffer has two lines "hello" and "world" stringsob_get_contents()
- returns output buffer content since last "bookmark" - returns "world", but buffer contents stay the samescripts ends and flushes whole output buffer to screen
if you use ob_get_clean()
instead it returns contents since last "bookmark" and deletes it from output buffer - the buffer has only "hello" in it
EDIT: I know it's very simple and naive explanation but I recall it helped me a lot to grasp the concept.