Reading line by line from STDIN

sobi3ch picture sobi3ch · Aug 15, 2012 · Viewed 70.5k times · Source

I want to do something like this:

$ [mysql query that produces many lines] | php parse_STDIN.php

In parse_STDIN.php file I want to be able to parse my data line by line from stdin.

Answer

Shiplu Mokaddim picture Shiplu Mokaddim · Aug 15, 2012

use STDIN constant as file handler.

while($f = fgets(STDIN)){
    echo "line: $f";
}

Note: fgets on STDIN reads the \n character.