2

Is it possible to print text on the screen using ob_start() & flush() and finally clean this text?

Now I have something like that:

<?php
ob_start();

ob_end_flush(); flush(); ob_flush();
for($i = 0; $i<100; $i++)
{
    echo $i.'<br>';
    flush();
    ob_flush();
}

ob_end_flush();
?>

and when I execute this code, I get something like that:

0
1
2
3
4
5
6
7
8
...

But I want echo current key and clean them:

0
... (clean "0")

1
... (clean "1")

2
... (clean "2")

Finally I would like to have only one number on the screen. Is it possible in PHP?

Thanks.

Majkson
  • 227
  • 4
  • 15
  • Can you elaborate on what exactly you are trying to achieve? Currently, to me, it looks like you are completely misusing `ob_*` functions. But it could also just be me, completely misinterpreting what you are trying to do. I will say though, that you can pass a callback to [`ob_start( $callback );`](https://secure.php.net/manual/en/function.ob-start.php#refsect1-function.ob-start-parameters). Not sure if that is what you are looking for. – Decent Dabbler Apr 21 '17 at 19:50
  • @DecentDabbler I just wanna to have on the screen always only one (current) key of for loop. So when loop is on the 34 key, on the screen I would like to have only "34", when the loop is on 56 key, on the screen I would like to have only "56" etc. That's all. – Majkson Apr 21 '17 at 19:53
  • 1
    Is this for CLI (command line interface), by any chance? Are you trying to have a timer on the command line and clear the line, every time the timer updates, perhaps? Because if it's for the web, you can't clean what's been sent out . The web doesn't work that way. – Decent Dabbler Apr 21 '17 at 19:55
  • @DecentDabbler exactly yes. – Majkson Apr 21 '17 at 19:56
  • Okay, but the `
    ` in your code appears to suggest you want to output HTML. Are you sure it's intended for the [command line](https://secure.php.net/manual/en/features.commandline.introduction.php) and not served through a webserver, like Apache?
    – Decent Dabbler Apr 21 '17 at 20:06
  • It's not important for me... I added "
    " because now I have one under the other. If you can tell me, how I can clean everything what was printed on the screen before current key of for loop - I don't need this "
    " tag.
    – Majkson Apr 21 '17 at 20:12
  • Okay, have a look at [this question](https://stackoverflow.com/questions/4320081/clear-php-cli-output) and see if any answers there provide what you are looking for. – Decent Dabbler Apr 21 '17 at 20:18
  • In that question is everything about "How to create progress info" but nothing about cleaning older instance/key of for loop... I want only know how I can print actually instance of for loop, thats all. Actually, in my code (which I paste in first post), "echo instance" cause I have printed instance from 0 to 99. I want only one instance, currenct instance of for loop, so when for loop is on 54 instance ($i == 54), then on the screen I want to see ONLY "54", when instance ($i == 67), I want to see on the screen ONLY "67" etc. Is it possible...? – Majkson Apr 21 '17 at 20:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142314/discussion-between-decent-dabbler-and-majkson). – Decent Dabbler Apr 21 '17 at 20:28
  • @Majkson I doubt this is possible via PHP(not sure if any other server side scripting supports this). Once content is echoed(not in buffer) on browser it cannot be removed. `ob_*` is meant for handling the content before the content is displayed, it won't help after the content is displayed. For exactly this purpose ajax(now socketio) is widely used. – Jigar Apr 22 '17 at 04:38

3 Answers3

2

Going by the comments of OP, I'm going to assume OP wants a timer on the command line and presume the <br> was meant to convey a newline.

A timer on the command line, which clears the line every tick, could be construed as follows:

for( $i = 0; $i < 100; $i++ ) {
  /*
    the \r moves the cursor back to the start of the line
    the additional spaces are there to make sure 
    any additional digits are removed as well
    adjust the amount of spaces to the max amount of digits
    (which is actually only useful if the timer was counting down, in stead of counting up)
  */
  echo "$i  \r";
  // wait a second
  sleep( 1 );
}

ob_* functions are of no use here; they are merely meant as an internal buffer for PHP, before it sends the output. You can't clear what's already been sent out.

If you want to completely clear the screen as well, I suggest to look at the suggestions in this question and related questions.

However, if you are outputting through a web server and viewing the result in a web browser, you are out of luck. You can't clear the output you have sent to a web browser, in the way you intend, because the web is a client <-> server architecture. You'll have to understand this architecture and use something like AJAX, websockets, or perhaps long polling.

Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
2

If you need to do it in console, @Decent Dabbler showed best suggestion. If you need to do it in the browser, you don't need PHP. You can make in using javascript.

function setCounter(value)
{
  document.getElementById('counter').innerText = value;
}

var counter = 0;
setInterval(
  function() {
    setCounter(++counter);
  },
  1000
);
<div id="counter"></div>
Leonid Zakharov
  • 940
  • 1
  • 6
  • 11
0

The solution is like e-commerce and basket and cookie and 99 times go request and answer between client and server. After every $i must have exited, and again request with the cookie and $i+1. And all like that to the end of looping.

Maybe this is what you want. On screen is only one number, last one, 99.

<?php
    ob_start();

    for($i = 0; $i<100; $i++)
    {
        echo $i.'<br>';  
        //flush();
        //ob_flush();
        ob_clean();
        echo $i.'<br>';
    }

    ob_end_flush();
    ?>
b2ok
  • 544
  • 6
  • 13
  • No, it's not that. I think, it's not hard to understand me what I want. I just want to inform user about actually progress in for loop. Something like percentage progress bar but I don't want JS scripts, CSS styles etc. I only want to print current key/instance of for loop. That's all... – Majkson Apr 21 '17 at 20:17
  • So I can help you. Add to your code `sleep(2)`. After that you can check - your code doesn't work because final result is on the end of loop. I want print result in real time. – Majkson Apr 21 '17 at 20:29