I had a few discussions regarding to use or not to use
STDIN in answer written in PHP. Reading from
STDIN is very expensive in PHP consuming 12 bytes:
fgetc(STDIN) // single character fgets(STDIN) // whole line
Different wordings in requirements
- take input from STDIN or an acceptable alternative
- If your language cannot take input from STDIN, it may take input from a hardcoded variable or suitable equivalent in the program
I went for "or an acceptable alternative". I figured it's about the technique, not forcebly use STDIN. This is one of the very few 'keep the code shorter' tricks PHP can use, especially compared to the other languages
Write the shortest program that takes one input (n) from STDIN (or equivalent) […]
My answer using
STDIN was also discussed:
Why not just
x=>x+<?=$x;? It's valid PHP4.1 and you can pass the values over POST, GET, SESSION, COOKIE, ... and it will work flawlessly. Or on PHP5.3 with register_globals=on (on your php.ini file).
Keeping in mind that
STDIN is available in PHP.
- Do you need to read from
STDINin requirements like
from STDIN or an acceptable alternativeor is any other method acceptable?
- Is a command line argument an acceptable alternative to
- Is using
If the last two methods are in fact acceptable, then all PHP answers could get rid of
fget[c/s](STDIN) and simply use
$a … $n based on the amount of input values.
This question is narrowed to PHP but the answers may apply for other languages as well.