PHP retrieving user input when executing a script from the command line

So either execute the script via the browser, http://someserver/somescript.php
or via the command line >php somescript.php
the example below shows how to support either eventuality

//first we check to see whether the script is being run via the command line, or via a browser

if(defined(‘STDIN‘) ){
echo “Running from CLI” . PHP_EOL;
echo “How many items would you like to batch?”;
$handle = fopen (“php://stdin”,”r”);
$line = fgets($handle);
$batchNumber = trim($line);
// and so on…
echo “Running from the Browser<br />”;
// and so on…

Tags: , , , , ,

Leave a Reply

You must be logged in to post a comment.