我正在編寫一個PHP CLI(命令行)腳本,如果它意外運行會造成一些不可逆轉的損壞。我想在繼續執行腳本之前顯示一個5秒的倒數計時器。我怎樣才能用PHP做到這一點?PHP 5秒倒計時(CLI,而不是JavaScript)
3
A
回答
3
要添加我的兩分錢,以下是如何添加確認提示。
<?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
(你必須鍵入「Y」,然後按Enter鍵。)
要刪除和替換的數量,而不是我在這裏做,儘量霧個Z聰明的解決方案。或者,你可以使用ncurses。見this tutorial。
4
您應該能夠使用睡眠
http://php.net/manual/en/function.sleep.php
像這樣的東西應該做的伎倆:
for($i = 5; $i > 0; $i--) {
echo "$i\n";
sleep(1);
}
echo "Doing dangerous stuff now...\n";
13
不要做一個倒計時。假定某人真的在觀看屏幕並閱讀/理解倒計時的含義。完全有可能有人走進來,坐在辦公桌的邊緣,然後對着腳本名字輸入,讓它在背部轉動時運行。
相反,用一些荒謬的命令行參數,允許破壞性模式:
$ php nastyscript.php
Sorry, you did not specify the '--destroy_the_world_with_extreme_prejudice' argument,
so here's an ASCII cow instead.
(__)
(oo)
/-------\/ Moooooo
/| ||
* ||----||
^^ ^^
$ php nastyscript.php --destroy_the_world_with_extreme_prejudice
Initiating Armageddon...
*BOOM*
ATH0++++ NO CARRIER
基本上是:
<?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's an ASCII cow instead.
(__)
(oo)
/-------\/ Moooooo
/| ||
* ||----||
^^ ^^
EOL;
3
1
這是我落得這樣做:
# from Wiseguy's answer
echo 'Continue? (Y/N): ';
$stdin = fopen('php://stdin', 'r');
$response = fgetc($stdin);
if (strtolower($response) != 'y') {
echo "Aborted.\n";
exit;
}
然而,對於一個漂亮的倒計時,這是我想出了:
/**
* 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)
}
通過使用\r
(回車)你可以從行的開始處開始並覆蓋當前行上的輸出。
相關問題
- 1. 5秒倒數計時器
- 2. PHP秒倒計時
- 3. Javascript秒數倒計時
- 4. PHP沒有秒倒計時
- 5. 秒倒計時
- 6. JavaScript時鐘,只倒計時小時,分鐘和秒..不是天
- 7. JavaScript倒計時(倒計時)毫秒太快,不會加載下一頁?
- 8. 倒計時秒數:毫秒
- 9. jQuery使凱利沙秒錶插件倒計時,而不是up
- 10. JavaScript倒計時,格式化秒到HH:MM:SS
- 11. JavaScript向後倒計時1秒暫停
- 12. 秒錶/倒計時(00:00:00)用javascript
- 13. 用倒計時顯示毫秒的javascript
- 14. PHP/JQuery/JavaScript倒計時
- 15. php javascript jquery倒計時?
- 16. 倒計時Javascript
- 17. 使用秒錶倒計時
- 18. NSTimer毫秒倒計時
- 19. 推秒倒數計時器
- 20. 分秒倒數計時器
- 21. gridView從120秒倒計時
- 22. 倒計時不會每秒更新
- 23. textview不顯示秒數倒計時Android
- 24. jQuery倒計時(分鐘:秒)秒
- 25. 從幾毫秒到幾分鐘秒的JavaScript倒計時腳本
- 26. 使用Javascript每秒鐘更新倒計時時間
- 27. Javascript/PHP倒數計時器,每秒更新並倒計時到特定的日期和時間
- 28. 倒計時GIF在電子郵件使用JavaScript不是PHP
- 29. 將AVPlayer倒帶5秒
- 30. jQuery倒計時插件倒計時秒數
提示確認而不是等待會更好嗎?如果這個人運行它然後看幾秒鐘? – drudge 2011-04-12 16:39:28
這是一個系統級功能,它可能會破壞性地執行嗎? – Wes 2011-04-12 16:40:09
這不會回答你的問題,但第二次我打開你的問題,「最終倒計時」開始在我聽的這個互聯網電臺上播放。 http://www.youtube.com/watch?v=9jK-NcRmVcw – Brad 2011-04-12 16:40:10