mpsn Posted November 25, 2011 Share Posted November 25, 2011 Hi, I need just quick run down on how to run a command line user interface for program for php using Windows command prompt: Let's say I have a calculator program: <?php class Calculator { function factorial($num) { if($num==1) return 1; else return $num*factorial($num-1); } //other functions... }//END CLASS Calculator ?> //Test runner: $calculator = new Calculator; $quit = false; while($quit != true) { echo "Please enter the number to find //how do i pass user's keyboard input to the function?? //so now pass user input to $calculator->factorial(//user input??); echo "Do you want to run program again?' //Again need to take in user input I feel it is similar to Java or C, but I just need some refresher, a little rustry, is it printf('%d', $quit) etc??? Quote Link to comment https://forums.phpfreaks.com/topic/251811-quick-rundonw-on-windows-command-prompt-to-run-php-program/ Share on other sites More sharing options...
kicken Posted November 26, 2011 Share Posted November 26, 2011 If you have a CLI build of php (not cgi) then there are three constants defined: STDIN - Input stream STDOUT - Output stream STDERR - Error stream You can use the normal file functions with these constants, such as fgets/fread. while($quit != true) { echo "Please enter the number to find "; $input = fgets(STDIN); //Do something with $input } Quote Link to comment https://forums.phpfreaks.com/topic/251811-quick-rundonw-on-windows-command-prompt-to-run-php-program/#findComment-1291264 Share on other sites More sharing options...
mpsn Posted November 26, 2011 Author Share Posted November 26, 2011 I checked in windows cmd: php -v and outputs: PHP 5.3.5 (cli)... and this is little script I'm trying to run: <?php //TEST CLI php $quit = false; function greet($name) { echo "Hello, $name! \n"; } while($quit != true) { echo "Please enter your name: "; $input = fgets(STDIN); greet($input); echo "Do you want to be re-greeted?"; $quit = fgets(STDIN); } ?> Please any help would be great Quote Link to comment https://forums.phpfreaks.com/topic/251811-quick-rundonw-on-windows-command-prompt-to-run-php-program/#findComment-1291269 Share on other sites More sharing options...
mpsn Posted November 26, 2011 Author Share Posted November 26, 2011 I didin't mention my problem in my previos post! In windows cmd, it outputs: cannot read input file:c:\dir\to\the\above\script Quote Link to comment https://forums.phpfreaks.com/topic/251811-quick-rundonw-on-windows-command-prompt-to-run-php-program/#findComment-1291272 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.