0

Can I clear all (already) echoed or printed items? I am NOT searching for output buffering. I am searching for an alternative.

Heres an example:

<?php

echo 'a';
print 'b';

The code I need

echo 'c';

The first two statements (echo and print) should not be present in the output
?>
Enrico König
  • 206
  • 1
  • 13
  • 22
  • Your question is a long way from clear. First you are going to have to tell us which environment you are running this code! The browser or the CLI. The second what do you mean by _the first two statements (echo and print) should not be present in the output_ How are they supposed to disappear – RiggsFolly Jan 03 '22 at 13:37
  • I want them to be "cleared" – Enrico König Jan 03 '22 at 13:38
  • 1
    From browser page or terminal screen? – RiggsFolly Jan 03 '22 at 13:38
  • Browser Page I mean the code doesnt just dissapear... I know that – Enrico König Jan 03 '22 at 13:39
  • 1
    nop you can't, echo write on output, so it is already ou of your programm if we can say. You can try on whitha sleep in yor terminal, you don't have control if it is out. Better use conditional and not to try to erase what you don't want as an ouput. – Armand Biestro Jan 03 '22 at 13:39
  • @nice_dev yep. Tnak you. But i will continue this post, so others can see it too :) Thank you! You are one real one. – Enrico König Jan 03 '22 at 13:45
  • 2
    An alternative to output buffering is to build your output as a string, then you can output the results when and where appropriate. – Nigel Ren Jan 03 '22 at 13:47
  • 1
    @Enripro Have you seen [`this answer`](https://stackoverflow.com/a/53042023/4964822) in the marked duplicate? Your post might also get deleted by the mods during moderation. Since your requirement is same as the marked duplicate question, there is no point in having the same question but different words. It is initially hard here on SO, but soon things will get better. – nice_dev Jan 03 '22 at 13:59

1 Answers1

1

No, whatever you write out is send on to the webserver, which sends it along to the browser. There is however a module in PHP called output buffering which decouples the output stream (temporarily).

Look into ob_start() and ob_end_clean()

<?PHP

ob_start(); // output buffering enabled

// these echos will be buffered in memory, instead of written out as they usually would:
echo 'A';
echo 'B';
echo 'C';

// now we've moved the buffer into `$html` (so that now contains 'ABC'), and we've stopped output buffering.
$html = ob_get_clean();

echo 'D'; // this is being send to the client/webbrowser as usual

echo $html; // now we print the ABC we intercepted earlier


So the client will receive : D A B C
Raxi
  • 2,452
  • 1
  • 6
  • 10
  • 1
    @Enripro Its all very well having a little moan about us, but if you ask a CLEAR well described question, you will get a clear and well described answer, if not you will get what may appear to you silly question, BUT THEY ARE NOT – RiggsFolly Jan 03 '22 at 13:46
  • @Enripro You have to remember, we are not mind readers and we are not looking over your shoulder. All we know about your situation is what you take the time to tell us. – RiggsFolly Jan 03 '22 at 13:50
  • @RiggsFolly Yeah... You are right. I still dont get, why everyone is just downvoting the "small" ones. Like its not going to help US. It may apear as a good feeling, when you do, but its really not a helpfull thing at all. (At least for me) – Enrico König Jan 03 '22 at 13:52
  • 1
    It 's not worth complaining about that. Stack Overflow comes with a very detailed guide on how to ask questions here. Everything that does not correspond to this is first criticized. Very few users read the guide on how to ask questions here. For this reason, there are always questions that leave far too much open and are simply undetailed. You should not take it too personally. – Marcel Jan 03 '22 at 13:52
  • @Enripro Once you get to a certain number of Reps, you are encouraged to assist in the curation of this site, with Downvotes and Close Votes. The idea being that a good question with its answer(s) will be a useful resource for other to search to get instant help without having to ask a question. Take a look at your question, do you believe that it would be in any way useful to others? That is the prime reason for the existance of SO afterall. – RiggsFolly Jan 03 '22 at 13:56