Is there a way for a PHP script running from the command line to request user input via keyboard?
Ex. Please enter your age: [cursor]
Thanks.
Command line: Request input?
Moderator: General Moderators
-
TS_Death_Angel
- Forum Commoner
- Posts: 31
- Joined: Sat Dec 31, 2005 8:49 am
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Several ways, enumerated in the PHP manual section on CLI scripting.
Personally, I prefer this on Windows systems:
I believe there is a constant for stdin when you're on Unix.
Personally, I prefer this on Windows systems:
Code: Select all
// set up streams
$GLOBALS['stdin'] = fopen('php://stdin', 'r');
// Sets up a prompt for command line scripts
function cli_prompt($length = 255) {
$line = fgets($GLOBALS['stdin'],$length);
return trim($line);
}
// ...
echo "Please enter your age: ";
$age = cli_prompt();-
TS_Death_Angel
- Forum Commoner
- Posts: 31
- Joined: Sat Dec 31, 2005 8:49 am