在PHP用户输入法做while循环?

时间:2018-08-05 09:42:12

标签: php while-loop scripting

我正在研究一个简单的脚本来翻译php中的ROT 13密码。我想制作一个循环脚本,该脚本将提示用户输入密文,将其翻译,然后继续循环直到他们按no。

由于某种原因,当循环第二次运行时,它完全跳过了fgets中的第一个输入提示,转到了文件底部的终止输入,我不知道为什么?我已经用Java编写了许多这样的程序。

这是相关的代码。

<?php

require('./rot_functions.php');

$input;
$terminate = false;

do {
echo "enter the cipher: \n";

$input = trim(fgets(STDIN));

$convertedString = [];

$input = str_split($input);

foreach ($input as $letter) {
    if (isCaps($letter)) {
        $convertedString[] = processCaps($letter);
    } else if (isLowerCase($letter)) {
        $convertedString[] = processLowerCase($letter);
    } else {
        $convertedString[] = $letter;
    }
}

echo implode($convertedString);

echo " would you like to continue? (y/n) \n";

$input = trim(fgetc(STDIN));

$input === 'y' ?: $terminate = true;

} while (!$terminate);

-- sample console output --
λ php rot13.php
enter the cipher:
Gur cnffjbeq vf 5Gr8L4qetPEsPk8htqjhRK8XSP6x2RHh
The password is 5Te8Y4drgCRfCx8ugdwuEX8KFC6k2EUu would you like to continue? 
(y/n)
 y
 enter the cipher:
 would you like to continue? (y/n)
 y
 enter the cipher:
 would you like to continue? (y/n)
 n

0 个答案:

没有答案