I am working on a simple script to translate a ROT 13 cipher in php. I wanted to make a looping script that would prompt the user to enter the cipher text, translate it and then continue looping until they pressed no.
For some reason when the loop runs for the second time it completely skips the first input prompt from fgets and goes to the terminating input at the bottom of the file and I cannot figure out why? I have written many programs like this in Java.
Here is the relevant code.
<?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