How to clear php STDIN before new input?

I have a nooby question about php-cli.

I use this:

define("STDIN", fopen('php://stdin','r')); $input = ""; while($input == "") { echo "Please enter : "; $input = fread(STDIN, 80); } 

Problem:

If I enter more than 80 characters, say 100, 20 additional characters will be added to the next.

How can I clear STDIN before each input?

+4
source share
1 answer

Set the length argument fread() to a higher value (i.e. 1024, 2048, 10000) - it determines the length max of the data read by fread() . If you only need up to 80 characters, then check that after reading and shortening, use substr() when necessary. You do not need to open the input stream if you use STDIN ( docs ), which is recommended due to errors when processing php://stdin before PHP 5.2.1 ( docs ).

 $input = ""; while($input == "") { echo "Please enter : "; $input = fread(STDIN, 10000); } 

Also note that STDIN , STDOUT , STDERR already defined system constants. You should not use these names for your own constants.

+2
source

Source: https://habr.com/ru/post/1447424/


All Articles