Output buffering - Echo values dynamically? - php

I want to echo string dynamically, not all of them at once when script finished running. Tried this one, but it echos all of them when script finished running. How can I echo values dynamically ?
<?php
ob_start();
echo "Line #1...<br>";
ob_flush();
flush();
sleep(2);
echo "Line #2...<br>";
ob_flush();
flush();
sleep(2);
echo "Line #4...<br>";
?>

Try sending a line-ending like \n or append at least 256 spaces to each echo to trigger the browser.
Some browsers will wait for at least 256bytes before rendering, others need a newline char. Try this combination before each flush:
echo str_repeat(" ", 256) . "\n";
Other cause could be the webserver that is caching the response.

Related

php - system command parameter output show results in one line using IE

i use this script to execute a shell from IE , while in the cli its been outputed correctly in the browser i see the results in one big line
<?php
$a = $_POST['a'];
$i=$_POST['i'];
$output = system("./xx.sh $i $a");
echo wordwrap($output,180,"<br />\n");
?>
Use this instead of wordwrap:
echo nl2br($output);
transforms line ending characters (\r\n) to <br />s
or combine:
echo wordwrap(nl2br($output), 180, "<br />\n");
or use <pre> for preformatted code:
echo "<pre>" . wordwrap($output, 180) . "</pre>";

Can't insert newlines

I have this code:
<?php
$ip = $_SERVER["REMOTE_ADDR"];
echo $ip;
echo "\n";
echo strftime('%c');
echo "\n";
echo date_default_timezone_get();
echo "\n";
?>
All three outputs should be on three separate lines, but they are all on same. What am I doing wrong?
If you want to see output on newlines in the browser use the html line break <br> instead of \n. Browsers collapse white spaces (\n is a white space) into a single ' '.
Browser interprets the output as html by default. If you want "see" the real output add this at the begin of file.
header("Content-type: text/plain");
or use a <br /> instead of simple new line
Just add The pre tag - Pre-formatted text <pre>
$ip = $_SERVER["REMOTE_ADDR"];
echo "<pre>";
echo $ip;
echo "\n";
echo strftime('%c');
echo "\n";
echo date_default_timezone_get();
echo "\n";
echo "</pre>";
php-output in the browser is formatted as Html. In Html you need the tag <br> to start a new line ... :-)
echo "<br>";
If you're viewing the page as HTML, then everything would be on one line. You need to add line breaks.
echo $ip . "<br />\n";
If you plan on viewing the output in HTML, you need to use HTML line-breaks.
<?php
$ip = $_SERVER["REMOTE_ADDR"];
echo $ip."<br>";
echo strftime('%c')."<br>";
echo date_default_timezone_get()."<br>";
?>

How can I make a timer animation in the command line?

This is a little silly, but something I've wanted to do before and could never figure out. I have a PHP script that runs from the command line. I'd like a little timer animation to let the user know the script is still running. Here's what I have:
while (1 == 1) {
echo '—';
usleep(100000);
echo '\';
usleep(100000);
echo '|';
usleep(100000);
echo '/';
}
But how do I get each echo to replace the character before it?
You need to print a backspace before each character, for example:-
echo '-';
usleep(100000);
echo "\b/";
Note that you have to use double quotes here or the escape sequence won't work.
I'm sure you can work the rest out :)
If "\b" doesn't work try:-
echo chr(8) . '/';
Try the php ncurses extension:
http://php.net/manual/en/ref.ncurses.php
You can use
echo "yourCharacter1\r";
usleep(100000);
echo "yourCharacter1\r";
or
echo "\ryourCharacter1\r";
usleep(100000);
echo "\ryourCharacter2\r";
\r sends the cursor back to position 0 on the same line.
Also you may try (found online)
system("clear"); // before you echo new characters
or
passthru('clear'); // before you echo new characters

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

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?
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.
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.

strange ob_start() behaviour - double output

ob_start() doesn't seem to be stopping any output so when I flush the buffer it's doubling up
<?php
ob_start();
echo "Text..... <br />";
echo ob_get_flush();
?>
Outputs
Text.....
Text.....
But I was expecting
Text.....
Any ideas ?
Thanks
Remove the echo on the last line.
ob_get_flush() implicitly prints the stored output and also returns it so you're printing it out twice.
You may have confused ob_get_flush() with ob_get_clean()
try:
<?php
ob_start();
echo "Text..... <br />";
ob_get_flush();
?>
from http://php.net/manual/en/function.ob-get-flush.php
Flush the output buffer, return it as a string and turn off output buffering
Flush the output means: it sends the output to the browser or the commandline.
return the string means: it returns the string, so you can store the flushed string in a variable. And since you're echoing this string you get the output a second time.

Categories