The codes of all other answers 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 carpet.
In short, at the end of the tests, I concluded that under Windows 10, independent of the PHP version, just print "\r"
quotes. Other commands used with the function chr()
or write CMD escape characters, it seems to have no effect.
Test with the function Chr()
<?php
echo time()."\r";
chr(27);
sleep(1);
echo time();
I put the sleep(1)
to test. No need to be there.
It is important that at the end of each printed line there is a return \r
in double quotes.
The original script was adapted from that reply: https://stackoverflow.com/a/11424357/1685571
In the same reply, the comment of @ninja recursion. because the original code uses \n
, which did not work. However, after switching to \r
worked perfectly.
I also reduced all the code to simplify and make it clearer where the "magic" is. I removed the constructor echo
because it makes no sense to use. The use of echo causes those strange symbols to be printed.
When executing was displayed 1484622554
. It was then replaced by 1484622555
and finally, 1484622556
.
That ^Z
is due to ENTER, CTRL + Z, ENTER
to execute the script.
Depending on the Windows version, it is CTRL + D
.
From this logic, it is possible to leave more dynamic creating a kind of Wraper for the scripts to be executed. An example of "Wraper" is in the original answer. Just need to remove unnecessary points and make some adjustments. Which are described here.
Curiosities:
Out of curiosity, I tested if it is really the Chr() function that cleans the line. I changed the value of the parameter to any other value and gave the same result. Then I tried to remove the function and to my surprise, it worked as expected.
<?php
echo time()."a\r";
//chr(0);
//sleep(1);
echo time()."b\r";
//chr(0);
//sleep(1);
echo time()."c";
On this test, it seems chr()
makes no difference. Printed the last value concatenated with "c" without printing the previous two.
Delimit the \r
with single quote, print the three concatenated lines.
With this test we can see that just print "\r"
in double quotes.
At least this works on Windows 10.
Common errors present in other answers is that almost all have the result printed by a constructor. This makes no sense and results in the printing of strange symbols like this �
.
Invoke the cmd /c cls
by function exec()
also makes no sense because the cls
shall be executed in another instance of CMD. This instance is empty and not visually accessible, that is, it is cleaning the invisible void. rsrs
Libraries of PHP
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 latest build is from 2012. Normally you don’t trust Ibraries of this type and the PHP manual itself warns. But nothing stops someone from continuing. Just read the source code, improve and compile.
CTRL+L does not clear?
– RFL
He wants to call from the @Rafaelacioly script
– gmsantos
system('clear');
, have tried?– RFL
system('clear'); gives as unrecognized command. I believe this is only for Linux
– Wallace Magalhães
It is a boring business to solve and in fact it is not portable even between versions of the same OS. But this is a consequence of using PHP in a way that it wasn’t meant to be used, so it does have these difficulties. Sometimes simple things get impossible in these scenarios.
– Bacco
I edited your question including the information you commented on in my question. In theory Windows 10 should have solved this. Using Powershell is an option in your case?
– Anthony Accioly
Try
system('cmd /c cls')
– LS_ᴅᴇᴠ
It worked! Ls_dev
– Wallace Magalhães
Wallacemagalhães, I saw that Oce changed the accepted answer from Ls_dev to Daniel’s. Did both answers work in your environment? Daniel’s was a better fit?
– Anthony Accioly
Exactly, Daniel’s fit better.
– Wallace Magalhães
I have tested all answers and in fact none of them solves what is asked in the title, the use of " r" returns the cursor to the beginning of the line which causes it to be overwritten, but this is not
cls
(clear screen) is just the current line.– Guilherme Nascimento