PHP stdin Standard Input Get

Getting stdin (Standard Input) in PHP

This article introduces how to get stdin (standard input) in the PHP programming language. Below, we introduce methods for reading one line, processing each line, and receiving and processing standard input as a whole, in that order. seq 3 | php -r "echo fgets…

Shou Arisaka
1 min read
Oct 27, 2025

This article introduces how to get stdin (standard input) in the PHP programming language. Below, we introduce methods for reading one line, processing each line, and receiving and processing standard input as a whole, in that order.

# Read one line
ASUS:/mnt/c/pg$ seq 3 | php -r "echo fgets(STDIN) ;"
1

# Process each line
ASUS:/mnt/c/pg/20190111030052$ seq 3 | php -r 'while($f = fgets(STDIN)){ echo "line: $f"; }'
line: 1
line: 2
line: 3

# Receive and process standard input as a whole
ASUS:/mnt/c/pg/20190111030052$ seq 3 | php -r '$stdins = "" ; while($f = fgets(STDIN)){ $stdins .= $f ; } echo $stdins ; '
1
2
3

# e.g.
ASUS:/mnt/c/pg/20190111030052$ seq 3 | php -r '$stdins = "" ; while($f = fgets(STDIN)){ $stdins .= $f ; } echo $stdins . "hogehoge" ; '
1
2
3
hogehoge

Share this article

Shou Arisaka Oct 27, 2025

🔗 Copy Links