The codes for all other responses did not work in my environment:
Windows 10, PHP7 and earlier versions up to version 5.3.6.
Of all the answers found on the internet, the most bizarre is to do dozens of line breaks. It's like sweeping dirt under the rug.
In summary, at the end of the tests, I concluded that under Windows 10, regardless of the version of PHP, just print "\r"
in quotes. Other commands used with the chr()
function or writing escape characters from the CMD seem to have no effect at all.
Test with the chr ()
<?php
echo time()."\r";
chr(27);
sleep(1);
echo time();
I put sleep(1)
to test. It is not necessary to be there.
It is important that at the end of each printed line you have a return \r
in double quotation marks.
The original script was adapted from this response: link
In that same response, it was important to comment on @ recursion.ninja since the original code uses \n
, which did not work. However, after switching to \r
it worked perfectly.
I also reduced all the code to simplify and clarify where the "magic" is. I removed the echo
constructor because it does not make sense to use. Using echo causes those foreign symbols to be printed.
Whenrunning,1484622554
wasdisplayed.Itwasthenreplacedby1484622555
andlast,1484622556
.
That^Z
isduetoENTER,CTRL+Z,ENTER
inordertoexecutethescript.
DependingontheversionofWindows,itisCTRL+D
.
Fromthislogic,itispossibletoleavemoredynamiccreatingakindofwraperforthescriptstobeexecuted.Anexampleofawraperisintheoriginalresponse.Youjustneedtoremoveunnecessarystitchesandmakesomeadjustments.Whicharedescribedhere.
Curiosities:
Outofcuriosity,Itestedifit'sreallythechr()functionthatdoesthelinecleanup.Imodifiedthevalueoftheparametertoanyothervalueandgavethesameresult.ThenItriedremovingthefunctionandtomysurprise,itworkedasexpected.
<?phpechotime()."a\r";
//chr(0);
//sleep(1);
echo time()."b\r";
//chr(0);
//sleep(1);
echo time()."c";
In this test, it seems like chr()
does not make any difference. You printed the last value concatenated with "c" without printing the previous two.
If you delimit \r
with single quotation mark, print the three concatenated lines.
With this test we can see that it is enough to only print "\r"
in double quotation marks.
At least this works in Windows 10.
Common mistakes in other responses mean that virtually everyone prints the output of a constructor. This does not make sense and causes the impression of strange symbols like this �
.
Invoking cmd /c cls
by the exec()
function also does not make sense since cls
will be executed in another instance of CMD . Instance that is empty and not visually accessible, that is, it is cleaning the invisible void. rsrs
PHP Libraries
In PHP there is a specific function to perform the function of clear/cls
. The function is ncurses_clear () .
This function belongs to the library NCurses and is available for Linux systems. However it is an experimental library and the last build is from 2012. Typically one does not trust libraries of this type and the PHP manual itself warns. But nothing prevents one from continuing. Just read the source code, refine and compile.