PHP 5秒倒计时(CLI,非JavaScript)
我正在编写一个PHPCLI(命令行)脚本,如果它意外运行,将造成一些不可逆转的损害。我想在继续执行脚本之前显示一个5秒的倒计时计时器。如何使用PHP实现这一点?您应该能够使用sleep 像这样的事情应该可以做到:PHP 5秒倒计时(CLI,非JavaScript),php,command-line-interface,Php,Command Line Interface,我正在编写一个PHPCLI(命令行)脚本,如果它意外运行,将造成一些不可逆转的损害。我想在继续执行脚本之前显示一个5秒的倒计时计时器。如何使用PHP实现这一点?您应该能够使用sleep 像这样的事情应该可以做到: for($i = 5; $i > 0; $i--) { echo "$i\n"; sleep(1); } echo "Doing dangerous stuff now...\n"; 不要倒计时。这就假定有人正在观看屏幕,并阅读/理解倒计时的含义。完全有可能有
for($i = 5; $i > 0; $i--) {
echo "$i\n";
sleep(1);
}
echo "Doing dangerous stuff now...\n";
不要倒计时。这就假定有人正在观看屏幕,并阅读/理解倒计时的含义。完全有可能有人走进来,坐在你书桌边上,用屁股键入脚本名,然后让脚本在他们转身时运行 相反,使用一些可笑的命令行参数来启用破坏性模式:
$ 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;
即使我1000%同意jnpcl要求确认而不是显示倒计时的评论,这里有一个在Windows命令行上测试过的解决方案(希望它能在*nix系统上运行):
要添加我的两分钱,以下是如何添加确认提示
<?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
这就是我最后要做的:
# 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
(回车符)可以从行的开头开始,覆盖当前行的输出。提示确认而不是等待是否更好?如果运行它的人看向别处几秒钟会怎么样?这是一个系统级的功能,它将执行,可能是破坏性的吗?这不会回答你的问题,但当我打开你的问题时,“最后倒计时”开始在我正在听的这个互联网电台播放。作为@jnpcl的替代方案,如果没有给出某个选项,我会停止并打印一条消息,例如,--force
@DarkDust:我会将其作为额外的保护措施,当然不是单独使用。当脚本仍在进行时,web服务器不一定会将其刷新到浏览器中。@ceejayoz True,虽然在本例中,询问者说它正在运行CLI。这是一个好主意(而且很有趣)。您是否介意详细说明如何在PHP脚本中实际实现这一点?+1提供了一个详细、易于理解且非常有趣的答案。如果可以,我会+3。在if
条件下,您是否打算在$response
周围放置strtolower()
而不是'y'
?
/**
* 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)
}