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

From Implement a Truth Machine

  • 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

From Write a Program that Writes a function BUT in a different language!

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.


  • 4
    \$\begingroup\$ 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. \$\endgroup\$ Commented Nov 11, 2015 at 11:07
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Nov 11, 2015 at 11:20
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – primo
    Commented Nov 11, 2015 at 11:22
  • \$\begingroup\$ @primo Also a very good point. I thought similar when I discussed this answer to "Summation from a to b". \$\endgroup\$ Commented Nov 11, 2015 at 11:27

2 Answers 2


You may use any acceptable input method

This means that any input that is allowed by the challenge (either explicitly in the challenge specification or implicitly in our I/O method defaults list (so long as the challenge doesn't explicitly disallow it)) is fair game. It can be STDIN, but it doesn't necessarily have to be.


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).

If you want solutions to be somewhat comparable and roughly same conditions for equally well equipped languages then defaulting to STDIN/STDOUT unless otherwise stated seems to be the most reasonable way to do it.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .