# PHP and “STDIN or an acceptable alternative”

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

My answer was discussed by Martin Büttner as I used a command line argument instead. Another answer ignored the requirements completely and the OP argued:

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). ## My questions Keeping in mind that STDIN is available in PHP. • Do you need to read from STDIN in requirements like from STDIN or an acceptable alternative or is any other method acceptable? • Is a command line argument an acceptable alternative to STDIN in? • Is using register_globals=on acceptable? If the last two methods are in fact acceptable, then all PHP answers could get rid of $argsv[n] and 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.

### Related

• Don't have time for a full answer right now, but "closest/acceptable alternative to STDIN" is usually intended to mean something like JavaScript's prompt(). Specifically, I think that command-line arguments are quite a different way of reading input. While they are allowed by default, if a question specifically asks for STDIN (or alternative), I'd assume they have a good reason for excluding command-line arguments. – Martin Ender Nov 11 '15 at 11:07
• @MartinBüttner That's what I thought. Will post a feature request on meta.stackexchange to make comments acceptable. :D Seriously, if you find the time a detailed answer would be great as it is a very good starting point. – insertusernamehere Nov 11 '15 at 11:20
• I think is safe to assume that most users will not have PHP5.3 or lower installed, on web server, with register_globals enabled - and won't take the time to set this up just to test a single submission. I think your answers will be less-well received as a result. I typically also assume -n (no .ini), which makes requiring a deprecated option a somewhat difficult position. – primo Nov 11 '15 at 11:22
• @primo Also a very good point. I thought similar when I discussed this answer to "Summation from a to b". – insertusernamehere Nov 11 '15 at 11:27

In my personal opinion you should be using STDIN (with fgets for example) for any input unless stated otherwise. That's something all languages should do, that have the capability of reading from STDIN/writing to STDOUT. Also, on other golfing sites (like anarchy golf) PHP programs will read from STDIN. (see http://golf.shinh.org/reveal.rb?join+lines/yzx_1175413094&php for example).