Jump to content

Quick rundonw on windows command prompt to run php program


mpsn

Recommended Posts

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???

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
}

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.