Is there an alternative to system('cls') in PHP Console?

Asked

Viewed 1,348 times

12

I’m using PHP (5.7) on the console (Windows 10), but I’m not able to clear the screen. I give a system('cls') and only a little square with a question mark appears, without cleaning the screen.

Problema com cls

I’ve done it this way:

for ($i = 0; $i < 50; $i++) echo "\r\n";

It cleans, but the next message gets down on the console and takes a while to clear, actually it just skips the lines, and I didn’t want that to happen. the system('cls') really cleans the console.

Is there any alternative or a way to solve this problem?

  • CTRL+L does not clear?

  • He wants to call from the @Rafaelacioly script

  • 1

    system('clear');, have tried?

  • 1

    system('clear'); gives as unrecognized command. I believe this is only for Linux

  • 4

    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.

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

  • 2

    Try system('cmd /c cls')

  • It worked! Ls_dev

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

  • Exactly, Daniel’s fit better.

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

Show 6 more comments

6 answers

9

  • It didn’t. The result is more or less like the system('cls'): a strange "little square" as if it were an unknown character.

  • That’s weird, I tested it here on Windows 10 and it worked. Wouldn’t it be something in your environment? Update the question with versions of PHP, Windows, relevant environment settings, and a printscreen of the control character if you can. When opening a prompt command and type cls or give echo in the exhaust code above the clean screen?

  • So, I use PHP 5.6, Windows 10. This happens: http://imgur.com/a/O7jRo. Must be some version problem, I will test with PHP 7 and edit here with the result.

5

cls is not a Windows executable, but a command from the command interpreter (cmd.exe on Windows 7, ...).

Thus, to be able to execute it must invoke the command interpreter:

cmd.exe /c cls

The argument /c force the interpreter to close after executing the command.

  • It didn’t work. It sounds like a great answer, but on Windows 8.1 and php 5.6 I didn’t have the expected result, see I created this script for testing: http://pastebin.com/raw/rX55LJN so I called it in cmd: php meuscript.php but it didn’t clear the screen https://i.stack.Imgur.com/u6N1y.jpg and if use system in place of exec the character is shown , see: https://i.stack.Imgur.com/eYwee.png

3

You can try it like this:

<?php
echo "Mensagem antes de limpar\n";

echo "\033[2J\033[1;1H";

echo "Mensagem depois de limpar\n";
?>

Where the ANSI \033[2J really cleans the screen, but its complementary \033[1;1H reset the start of the 'next screen', you can test only with \033[2J, but you will see that the text printed after that echo will be a little below the start of the window prompt.

This solution also works on the Linux, only in the Power Shell that I tested and it didn’t work.

The result will be this:

inserir a descrição da imagem aqui

2


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.

inserir a descrição da imagem aqui

When executing was displayed 1484622554. It was then replaced by 1484622555 and finally, 1484622556.

inserir a descrição da imagem aqui

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.

  • 1

    Daniel, the other answers work if exhaust sequences are enabled. See that the OP itself marked an answer as correct. Additionally I can confirm that exhaust sequences worked properly for me in Powershell. That said Iss, Unfortunately there are incompatibilities between the PHP interprador and cmd as placed by Bacco. In some solutions staff is required to appeal to loops with \r\n :(

0

Try this: popen('cls || clear','w');

It will work because || means "or" remember in c? Well, something else the cls command doesn’t work on Linux should use clear. Try and let me know if it worked!

-1

Unfortunately, PHP 8.0.2 does not have a function to do this. However, if you just want to clear the console, try this: print (" 033 [2J 033 [; H"); or popen('cls','w'); Works in php 8.0.2. Same as system('cls') using c language programming inserir a descrição da imagem aqui

  • I ended up confusing because I published this same answer in other communities. I will put the answer in Portuguese.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.