PHP standard input?

PhpFile IoStdin

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

It is possible to read the stdin by creating a file handle to php://stdin and then read from it with fgets() for a line for example (or, as you already stated, fgetc() for a single character):

<?php
$f = fopen( 'php://stdin', 'r' );

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

fclose( $f );
?>

Solution 2 - Php

Reading from STDIN is recommended way

<?php
while (FALSE !== ($line = fgets(STDIN))) {
   echo $line;
}
?>

Solution 3 - Php

To avoid having to mess around with filehandles, use file_get_contents() and php://stdin:

$ echo 'Hello, World!' | php -r 'echo file_get_contents("php://stdin");'
Hello, World!

(If you're reading a truly huge amount of data from stdin you might want to use the filehandle approach, but this should be good for many megabytes.)

Solution 4 - Php

A simple method is

$var = trim(fgets(STDIN));

Solution 5 - Php

Grab it all in one shot:

$contents = file_get_contents("php://stdin");
echo $contents;

Solution 6 - Php

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

$f = fopen('php://stdin', 'r');

Solution 7 - Php

This also works:

$data = stream_get_contents(STDIN);

Solution 8 - Php

IIRC, you may also use the following:

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

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

Solution 9 - Php

When using fgets, it may block in bash scripts, if the stdin isn't set or empty, including while using the @ php error control operator.

#!/usr/bin/php
<?php
$pipe = @trim(fgets(STDIN));
// Script was called with an empty stdin
// Fail to continue, php warning 

This behavior can be avoided by setting stream_set_blocking on the php header:

#!/usr/bin/php
<?php
stream_set_blocking(STDIN, false);
$pipe = @trim(fgets(STDIN));
// Script was called with an empty stdin
// No errors or warnings, continue 
echo $pipe . "!";

As example, to be called as follow:

echo "Hello world" | ./myPHPscript
// Output "Hello world!"
./myPHPscript
// Output "!"

Solution 10 - Php

Instead of manually opening STDIN stream, use built in readline() function if you just want to read a single line without too much a hassle :

<?php
$age= readline("Enter your age: ");
echo "Your age is : ".$age;

PHP documentation is your friend : https://www.php.net/manual/en/function.readline.php

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris LutzView Question on Stackoverflow
Solution 1 - PhpPatrick GlandienView Answer on Stackoverflow
Solution 2 - Phpanatoly techtonikView Answer on Stackoverflow
Solution 3 - PhpmjsView Answer on Stackoverflow
Solution 4 - PhpMuhammad UsmanView Answer on Stackoverflow
Solution 5 - PhpredolentView Answer on Stackoverflow
Solution 6 - PhpGregView Answer on Stackoverflow
Solution 7 - PhpRichard CrossView Answer on Stackoverflow
Solution 8 - PhpFritz HView Answer on Stackoverflow
Solution 9 - PhpNVRMView Answer on Stackoverflow
Solution 10 - PhpDonovan PView Answer on Stackoverflow