63

Is it possible to get input from a user using php cli and then dump the input into a variable and then the script goes ahead.

Just like the c++ cin function ?

Is that possible if yes then how ? Maybe not only php but maybe with some linux commands ?

yivi
  • 1
  • 16
  • 86
  • 115
kritya
  • 3,242
  • 6
  • 41
  • 59

4 Answers4

98

To read a line from standard input in php CLI mode, you can do:

$fin = fopen ("php://stdin","r");
$line = fgets($fin);

On older versions of PHP STDIN constant may also work

$line = fgets(STDIN);

To read all the content from input use:

file_get_contents('php://input');
anubhava
  • 713,503
  • 59
  • 514
  • 593
  • 2
    Simply append another line like this: `$line2 = fgets(STDIN);` – anubhava Jul 01 '11 at 05:38
  • 4
    And if you want to read in a loop until EOF then use `while (FALSE !== ($line = fgets(STDIN))) { echo "line=$line"; }` – anubhava Jul 01 '11 at 05:39
  • 2
    `STDIN` is not always defined so the accepted answer is better. – laurent Mar 29 '13 at 06:50
  • 5
    In source code of CORE php > 5.3.6 it is defined as: ```define('STDIN', fopen('php://stdin', 'r')); define('STDOUT', fopen('php://stdout', 'w')); define('STDERR', fopen('php://stderr', 'w'));``` – Arthur Kushman Oct 11 '16 at 09:36
  • 1
    I realize this is a very old answer but `STDIN` is not defined in any modern environment I've used. – Eaten by a Grue Apr 11 '22 at 16:31
  • 1
    @EatenbyaGrue: Thanks for your comment. I haven't visited this answer in all these years. I have edited it to make it work with newer php versions. – anubhava Apr 11 '22 at 16:55
83

Have a look at this PHP manual page http://php.net/manual/en/features.commandline.php

in particular

<?php
echo "Are you sure you want to do this?  Type 'yes' to continue: ";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
if(trim($line) != 'yes'){
    echo "ABORTING!\n";
    exit;
}
echo "\n";
echo "Thank you, continuing...\n";
?>
Devraj
  • 2,995
  • 22
  • 25
8

In this example I'm extending Devjar's example. Credits for him for example code. Last code example is simplest and safest in my opinion.

When you use his code:

<?php
echo "Are you sure you want to do this?  Type 'yes' to continue: ";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
if(trim($line) != 'yes'){
echo "ABORTING!\n";
exit;
}
echo "\n";
echo "Thank you, continuing...\n";
?>

You should note stdin mode is not binary-safe. You should add "b" to your mode and use following code:

<?php
echo "Are you sure you want to do this?  Type 'yes' to continue: ";
$handle = fopen ("php://stdin","rb"); // <-- Add "b" Here for Binary-Safe
$line = fgets($handle);
if(trim($line) != 'yes'){
echo "ABORTING!\n";
exit;
}
echo "\n";
echo "Thank you, continuing...\n";
?>

Also you can set max charters. This is my personal example. I'll suggest to use this as your code. It's also recommended to use directly STDIN than "php://stdin".

<?php
/* Define STDIN in case if it is not already defined by PHP for some reason */
if(!defined("STDIN")) {
define("STDIN", fopen('php://stdin','rb'))
}

echo "Hello! What is your name (enter below):\n";
$strName = fread(STDIN, 80); // Read up to 80 characters or a newline
echo 'Hello ' , $strName , "\n";
?>
Niko9911
  • 321
  • 5
  • 13
0

similarly you could create a function, like python.

$line = input("Please put in a number: ");
if ($line === 20){
    echo true;
} else {
    echo false;
}

function input(string $prompt = null): string
{
    echo $prompt;
    $handle = fopen ("php://stdin","r");
    $output = fgets ($handle);
    return trim ($output);
}