Question

Is there any way I can take input from user while running a PHP script via Command line interface?

No correct solution

OTHER TIPS

Waits for a return (newline):

$handle = fopen ('php://stdin', 'r');
$input  = fgets($handle);

For a single character such as a menu choice:

$input  = fgetc($handle);

just type

$line = fgets(STDIN);

example :

<?php
echo "What's your name ? ";
$line = fgets(STDIN);
echo "hi $line";

in your console :

php demo.php
What's your name ? Arrobe
hi Arrobe

fgets open a file in read mode

STDIN (for Standard Input) in a special file : your keyboard

source : https://www.php.net/manual/en/features.commandline.io-streams.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top