I want to write a PHP script that I can use from the command line. I want it to prompt and accept input for a few items, and then spit out some results. I want to do this in PHP, because all my classes and libraries are in PHP, and I just want to make a simple command line interface to a few things.
The prompting and accepting repeated command line inputs is the part that's tripping me up. How do I do this?
One line of code (line 2):
Checkout this answer's source in blog post How can I capture user input from the cmd line using PHP?.
Simple:
I'm not sure how complex your input might be, but readline is an excellent way to handle it for interactive CLI programs.
You get the same creature comforts out of it that you would expect from your shell, such as command history.
Using it is as simple as:
If available, it really does make it simple.
Here a typical do-case-while for console implementation:
where user can use arrows (up and down) to access the history.
My five cents: Using
STDOUT
andSTDIN
:From PHP: Read from Keyboard – Get User Input from Keyboard Console by Typing:
I found an example on PHP.net, Utiliser PHP en ligne de commande: