what happened when i use multi ob_start() without ob_end_clean() or ob_end_flush()?
Asked Answered
S

2

18

i have reviewed php manual about the ob_start() ob_end_clean() ob_end_flush(). And i have seen a different example about the subject, anyway i modified the example but i'm confused at this point. here is the script.

ob_start();
echo "Hello x, ";

ob_start();
echo "Hello y, ";

ob_start();
echo "Hello z, ";

ob_start();
echo "Hello World";
$ob_2 = ob_get_contents();
ob_end_clean();

echo "Galaxy";
$ob_1 = ob_get_contents();
ob_end_clean();

echo " this is OB_1 : ".$ob_1;
echo "<br>  and this is OB_2  : ".$ob_2;

And output of this script is:

Hello x, Hello y, this is OB_1 : Hello z, Galaxy

and this is OB_2 : Hello World

--------------------------------------------

Why the output isn't like that?

this is OB_1 : Hello x, Hello y, Hello z, Galaxy

and this is OB_2 : Hello World

And what is the point i have missed?

Spurling answered 4/5, 2012 at 1:8 Comment(0)
D
23

Output buffers work like a stack. You create one buffer and echo "Hello x, " into it, then you create another buffer and echo "Hello y " into it, then you create a third buffer and echo "Hello z, " into it. The "Hello World" goes into a fourth buffer, which is closed by the call to ob_end_clean(), so you're back to the third one. When you call ob_get_contents() after echoing "Galaxy", you're getting the contents of that third buffer.

If you call ob_get_contents() again at the end of this code, you'll get the "Hello y, " that's in the second buffer. And if you ob_end_close() that and then ob_get_contents() again, you'll get the "Hello x, " from the first buffer.

Dre answered 4/5, 2012 at 1:18 Comment(1)
I am trying to use flush(), ob_flush() etc. for one hour, trying to find helpful sources. You are the first one who explained what is going on. +1 for a simple but super helpful answer.Buna
G
46

I'll annotate your code to explain what is going on. All output buffers are initialised to be empty, that is standard:

ob_start(); // open output buffer 1
echo "Hello x, "; // echo appended to output buffer 1

ob_start(); // open output buffer 2
echo "Hello y, "; // echo appended output buffer 2

ob_start(); // open output buffer 3
echo "Hello z, "; // echo appended to output buffer 3

ob_start(); // open output buffer 4
echo "Hello World"; // echo appended output buffer 4
$ob_2 = ob_get_contents(); // get contents of output buffer 4
ob_end_clean(); // close and throw away contents of output buffer 4

echo "Galaxy"; // echo appended to output buffer 3
$ob_1 = ob_get_contents(); // get contents of output buffer 3
ob_end_clean(); // close and throw away contents of output buffer 3

// at this point, $ob_2 = "Hello World" and $ob_1 = "Hello z, Galaxy"
// output buffer 1 = "Hello x," and output buffer 2 = "Hello y,"

echo " this is OB_1 : ".$ob_1; // echo appended to output buffer 2

// output buffer 2 now looks like "Hello y,  this is OB_1 : Hello z, Galaxy" 

echo "<br>  and this is OB_2  : ".$ob_2; // echo appended to output buffer 2

// output buffer 2 now looks like:
//   "Hello y,  this is OB_1 : Hello z, Galaxy<br> and this is OB_2  : Hello World"

// output buffer 2 implicitly flushed by end of script
// output from buffer 2 captured by (appended to) output buffer 1
// output buffer 1 now looks like:
//   "Hello x, Hello y,  this is OB_1 : Hello z, Galaxy<br> and this is OB_2  : Hello World"
// output buffer 1 implicitly closed by end of script. This is when your output
// actually gets printed for this particular script.
Gaiety answered 4/5, 2012 at 1:22 Comment(0)
D
23

Output buffers work like a stack. You create one buffer and echo "Hello x, " into it, then you create another buffer and echo "Hello y " into it, then you create a third buffer and echo "Hello z, " into it. The "Hello World" goes into a fourth buffer, which is closed by the call to ob_end_clean(), so you're back to the third one. When you call ob_get_contents() after echoing "Galaxy", you're getting the contents of that third buffer.

If you call ob_get_contents() again at the end of this code, you'll get the "Hello y, " that's in the second buffer. And if you ob_end_close() that and then ob_get_contents() again, you'll get the "Hello x, " from the first buffer.

Dre answered 4/5, 2012 at 1:18 Comment(1)
I am trying to use flush(), ob_flush() etc. for one hour, trying to find helpful sources. You are the first one who explained what is going on. +1 for a simple but super helpful answer.Buna

© 2022 - 2024 — McMap. All rights reserved.