How can clear screen in php cli (like cls command)
For Windows users :
system('cls');
For Linux users : system('clear');
Clear PHP CLI output
Try outputting a line of text and terminating it with "\r" instead of "\n".
The "\n" character is a line-feed which goes to the next line, but "\r" is just a return that sends the cursor back to position 0 on the same line.
So you can:
echo "1Done\r";
echo "2Done\r";
echo "3Done\r";
etc.Make sure to output some spaces before the "\r" to clear the previous contents of the line.
[Edit] Optional: Interested in some history & background? Wikipedia has good articles on "\n" (line feed) and "\r" (carriage return)
PHP CLI, Windows 7 CMD, and CLS/FF need to get along. (Clear Win7 CMD with PHP from CLI)
This is the only, yet very ugly, way of doing this, that I found working so far:
public function clearStdin()
{
for ($i = 0; $i < 50; $i++) echo "\r\n";
}
Clear CMD-shell with php
How about this?
<?php
$i = 1;
echo str_repeat("\n", 300); // Clears buffer history, only executes once
while(1)
{
echo "test_".$i."\r"; // Now uses carriage return instead of new line
sleep(1);
$i++;
}
the str_repeat()
function executes outside of the while
loop, and instead of ending each echo with a new line, it moves the pointer back to the existing line, and writes over the top of it. How to update (clear previous value) screen with PHP?
After
echo $a;
You can add echo exec('clear');
Clear CMD-shell with php
How about this?
<?php
$i = 1;
echo str_repeat("\n", 300); // Clears buffer history, only executes once
while(1)
{
echo "test_".$i."\r"; // Now uses carriage return instead of new line
sleep(1);
$i++;
}
the str_repeat()
function executes outside of the while
loop, and instead of ending each echo with a new line, it moves the pointer back to the existing line, and writes over the top of it.
Related Topics
Denormalize Nested Structure in Objects with Symfony 2 Serializer
Object Copy Versus Clone in PHP
Adding an Admin Page on Opencart Version 2
How to Retrieve Date from Table Cell Using PHPspreadsheet
Exploding by Array of Delimiters
PHP Error: Cannot Use Object of Type Stdclass as Array (Array and Object Issues)
Optimizing MySQL Fulltext Search
How Would You Transform a Pre-Existing Web App into a Multilingual One
How to Run PHP File Using Cron Jobs
Function to Add Dashes to Us Phone Number in PHP
Best Way to Identify a User Uniquely
PHP Search Array Key and Get Value
Get User Data Using Access Token in Laravel Passport Client App
Yii Framework: Using Data from Related Active Record Models for Searching
How to Pass an Array of Checked/Unchecked Checkbox Values to PHP Email Generator
Customizing My-Account Addresses Fields in Woocommerce 3
How to Determine the Length (In Pixels) of a String Being Rendered on a Web Page