How to get CLI Input in PHP?

Php Uses I/O Stream To Get input.

fopen ("php://stdin","r") // open a read stream

So to get a input from user in cli.

echo "Type Something: "
$r_stream = fopen ("php://stdin","r");
$input = trim(fgets($r_stream));
echo = "\n";
echo "You Entered ".$input;

You can run the php file by opening your CMD in a Windows machine or Terminal in Linux based machine

And Getting to the directory in which file is stored and typing the command below.

php file_name.php

Leave a Reply

Your email address will not be published. Required fields are marked *