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

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.