tags:

views:

1427

answers:

3

I know PHP is usually used for web development, where there is no standard input, but PHP claims to be usable as a general-purpose scripting language, if you do follow it's funky web-based conventions. I know that PHP prints to stdout (or whatever you want to call it) with print and echo, which is simple enough, but I'm wondering how a PHP script might get input from stdin (specifically with fgetc(), but any input function is good), or is this even possible?

+3  A: 

You can use fopen() on php://stdin:

$f = fopen('php://stdin', 'r');
Greg
+10  A: 
Patrick Daryll Glandien
You could also use the predefined constant STDIN instead of opening it manually: $line = fgets(STDIN);
gix
STDIN did not work for me, but 'php://stdin', 'r' did. Using PHP 5.2.9-2 (cli) (built: Apr 9 2009 08:23:19) on Vista.
Eric J.
+4  A: 

IIRC, you may also use the following:

$in = fopen(STDIN, "r");
$out = fopen(STDOUT, "w");

Technically the same, but a little cleaner syntax-wise.

Fritz H