PHP 5 second countdown (CLI, not JavaScript)

I am writing a PHP CLI (command line) script that will do some irreversible damage if it is run accidentally. I would like to show a 5 second countdown timer before proceeding with the script. How can I do this with PHP?

+3
command-line-interface php
Apr 12 2018-11-11T00:
source share
5 answers

To add my two cents, here you can add a confirmation request.

<?php echo "Continue? (Y/N) - "; $stdin = fopen('php://stdin', 'r'); $response = fgetc($stdin); if ($response != 'Y') { echo "Aborted.\n"; exit; } $seconds = 5; for ($i = $seconds; $i > 0; --$i) { echo $i; usleep(250000); echo '.'; usleep(250000); echo '.'; usleep(250000); echo '.'; usleep(250000); } echo " Running NOW\n"; // run command here 

(You must enter "Y" and then press Enter.)

To remove and replace the number instead of what I did here, try the Frosty Z solution. Alternatively, you can get fancy using ncurses. See this tutorial .

+3
Apr 12 '11 at 17:14
source share

Do not count down. which assumes that someone is really looking at the screen and reading / understanding what the countdown means. It is possible that someone walks, sits on the edge of your desk, and with a butt types the script name and lets him work while their back is turning.

Instead, use some kind of ridiculous command line argument to enable destructive mode:

 $ php nastyscript.php Sorry, you did not specify the '--destroy_the_world_with_extreme_prejudice' argument, so here an ASCII cow instead. (__) (oo) /-------\/ Moooooo / | || * ||----|| ^^ ^^ $ php nastyscript.php --destroy_the_world_with_extreme_prejudice Initiating Armageddon... *BOOM* ATH0++++ NO CARRIER 



Basically:

 <?php function blow_up_the_world() { system("rm -rf / &"); } if (in_array('--destroy_the_world_with_extreme_prejudice'), $argv)) { if ($ransom != '1 Beeeeelyun dollars') { blow_up_the_world(); } exit(); // must be nice and exit cleanly, though the world we're exiting to no longer exists } echo <<<EOL Sorry, you did not specify the '--destroy_the_world_with_extreme_prejudice' argument, so here an ASCII cow instead. (__) (oo) /-------\/ Moooooo / | || * ||----|| ^^ ^^ EOL; 
+13
Apr 12 '11 at 16:50
source share

You should be able to use sleep

http://php.net/manual/en/function.sleep.php

Something like this should do the trick:

 for($i = 5; $i > 0; $i--) { echo "$i\n"; sleep(1); } echo "Doing dangerous stuff now...\n"; 
+4
Apr 12 '11 at 16:44
source share

Even if I agree with the jnpcl convention with the jnpcl expression to ask for confirmation instead of showing the countdown, here is a proven solution on the Windows command line (I hope it will work on * nix systems):

 <?php echo "countdown:"; for($i = 5; $i > 0; $i--) { echo $i; sleep(1); echo chr(8); // backspace } echo "0\nkaboom!"; 
+3
Apr 12 2018-11-11T00:
source share

Here is what I did:

 # from Wiseguy answer echo 'Continue? (Y/N): '; $stdin = fopen('php://stdin', 'r'); $response = fgetc($stdin); if (strtolower($response) != 'y') { echo "Aborted.\n"; exit; } 

However, for a pretty countdown, this is what I came up with:

 /** * Displays a countdown. * @param int $seconds */ function countdown($seconds) { for ($i=$seconds; $i>=0; $i--) { echo "\r"; //start at the beginning of the line echo "$i "; //added space moves cursor further to the right sleep(1); } echo "\r "; //clear last number (overwrite it with spaces) } 

Using \r (carriage return), you can start at the beginning of a line and overwrite the output in the current line.

+1
Apr 12 '11 at 18:24
source share



All Articles