The following is not achieving what I desire
<?
echo ob_start() . "<br>";
echo "1x<br>";
echo ob_start() . "<br>";
echo "2x<br>";
echo ob_flush() . "<br>";
echo "3x<br>";
echo ob_flush() . "<br>";
?>
The output is the following
1
1x
1
2x
1
3x
1
I am wanting something along the lines of
1x
3x
2x
I assume the problem is its putting the output from the second ob_start() in the first output buffer. But how do I get my desired output?
Basically what I am trying to achieve is providing the tag which needs to be in the head of a HTML document at a latter point in the output. Ie, half way through the script after it has already printed the docs head infomation it needs to then provide the .
Refer to the PHP manual for ob_start . You don't want to
echo ob_start();
because that function returns true or false, so it will output a 1 or 0 instead
ob_start();
echo "1x" . "<br />";
echo "2x" . "<br />";
echo "3x" . "<br />";
ob_flush();
Overall your objective isn't very clear. ob_start() is used for cleaning up a bunch of output before it is sent. It shouldn't be used as a stack.
Try SplStack if you want to use a stack in PHP.
Why don't you just write
echo "1x"."<br>";
echo "3x"."<br>";
echo "2x"."<br>";
What about the following:
<?php
echo ob_start();
echo "1x<br>";
$keep_me_1 = ob_get_contents(); /* optional and for later use */
echo ob_flush();
echo ob_start();
echo "3x<br>";
$keep_me_2 = ob_get_contents(); /* optional and for later use */
echo ob_flush();
echo ob_start();
echo "3x<br>";
$keep_me_3 = ob_get_contents(); /* optional and for later use */
echo ob_flush();
?>
If you want to use more of the "stack" functionality you should take a look at ob_end_flush
.
Your issue is that ob_*
functions should not be echo
'ed.
<?
ob_start() . "<br>";
echo "1x<br>";
ob_start() . "<br>";
echo "2x<br>";
ob_flush() . "<br>";
echo "3x<br>";
ob_flush() . "<br>";
?>
ob_start() function returns a boolean. So basically, your code is simply echoing a true
value, which translates as 1
when converted to a string.
You can use ob_get_contents() to save the contents of the inner buffer to a string, then call ob_end_clean() to throw the contents away. Later, use a callback function in the outer buffer to write out the string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.