How to read console/user input in PHP?

I think you are looking for the readline function

$number = readline("Enter a number: ");
echo 'You picked the number: '.$number;

http://www.php.net/manual/en/function.readline.php


If you don't have readline installed, -or- you're writing a library, you should do this:

if (!function_exists('readline')) {
    function readline($question)
    {
        $fh = fopen('php://stdin', 'r');
        echo $question;
        $userInput = trim(fgets($fh));
        fclose($fh);

        return $userInput;
    }
}

$age = readline('What is your age? ');
echo "You are $age years old.\n";

Tags:

Php

Terminal