Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a command-line interactive PHP script?

Tags:

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?

like image 295
user151841 Avatar asked May 28 '10 14:05

user151841


1 Answers

The I/O Streams page from the PHP manual describes how you can use STDIN to read a line from the command line:

<?php  $line = trim(fgets(STDIN)); // reads one line from STDIN  fscanf(STDIN, "%d\n", $number); // reads number from STDIN ?> 
like image 151
Justin Ethier Avatar answered Oct 18 '22 12:10

Justin Ethier