0
ob_start();

for($i=1; $i<=10; $i++){
  echo 'FUU';
  $output = ob_get_contents();
}

echo $output;
ob_end_flush();  

So instead of one FUU I get 10! Why does my ob_start() not work?

2
  • One? Ten? Shouldn't you get twenty? You're echoing it ten times into your output buffer, then you're echoing $output, which also has it ten times, into your output buffer, then you're flushing the buffer, which which should output both sets of ten. Commented Apr 24, 2011 at 14:00
  • could be, I saw a lot of them :) Commented Apr 24, 2011 at 14:01

1 Answer 1

2

I don't think you get what it's doing. Here's a walk through:

$i = 1
Output buffer = FUU
ob_get_contents()/$output = FUU

$i = 2
Output buffer = FUUFUU
ob_get_contents()/$output = FUUFUU

$i = 3
Output buffer = FUUFUUFUU
ob_get_contents()/$output = FUUFUUFUU

When you call ob_get_contents each iteration, it's getting everything that has been echoed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.