PHP cli getting input from user and then dumping into variable possible?

Php

Php Problem Overview


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 ?

Php Solutions


Solution 1 - Php

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');

Solution 2 - Php

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";
?>

Solution 3 - Php

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";
?>

Solution 4 - Php

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);
}

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
QuestionkrityaView Question on Stackoverflow
Solution 1 - PhpanubhavaView Answer on Stackoverflow
Solution 2 - PhpDevrajView Answer on Stackoverflow
Solution 3 - PhpNiko9911View Answer on Stackoverflow
Solution 4 - PhpWolfDen133View Answer on Stackoverflow