If the user types in multiple characters when fgetc(STDIN) is called, another call to fgetc(STDIN) won’t ever ask for a new character and will use the next character in the buffer. One way I solved this was creating the following function:
<?php
function get_char_from_stdin() {
if (($char = fgetc(STDIN)) != "\n")
fgets(STDIN);
return $char;
}
?>
Is there a more elegant solution than the above?
P.S. Please pardon me if my terminology is off. Please correct any misused terms. Thanks.
EDIT: Does this work on Windows? I’m using a Mac right now.
STDIN is buffered so in order to get the first char entered after you called your function, you have to open a new stream with an empty buffer.