I have a nooby question about php-cli.
I’m using this :
define("STDIN", fopen('php://stdin','r'));
$input = "";
while($input == "") {
echo "Please enter : ";
$input = fread(STDIN, 80);
}
Problem is :
If I enter more than 80 characters, say 100, the 20 extras characters are added to the next one.
How can i “clear” the STDIN before each input ?
Set
lengthargument offread()to higher value (i.e. 1024, 2048, 10000) – it determines max lenght of data read byfread(). If you need just up to 80 characters, then check that after the read and shorten usingsubstr()when needed. You do not need to open the input stream if you useSTDIN(docs) which is recommended due to bugs in handling ofphp://stdinup to PHP 5.2.1 (docs).Also bear in mind
STDIN,STDOUT,STDERRare already defined system constants. You should not use these names for your own constants.