Categories
Uncategorized

Controlling cursor position in PHP CLI applications (and others)

I realized that this is still a frequently asked question on forums – how do I move the cursor in my PHP console application? The simplest way to do it in Unix based systems is to echo the terminal control escape sequence. This will work for any terminal application.

for ($i = 1; $i < 11; $i++) {
    echo "$i\n";
    sleep(1);
    echo "\033[1A";
}

For the full list of the terminal control codes (escape sequences) for cursor and other features, like color – refer to: http://php.net/manual/en/book.ncurses.php take a look at ncurses library.