Page 1 of 1
Can PHP CLI perform cout/cin type Fuctionality?
Posted: Sun May 19, 2002 5:07 pm
by techniq
I would like to request user input from the CLI by prompting for it. Then they type it in and I could assign their input to a variable(s). Does php provide functionality like 'cout <<' & 'cin >>' ? If yes, can anyone tell me how.
Posted: Sun May 19, 2002 6:53 pm
by volka
a similar question has been posted
here
and take a look
here
especially "php://stdin", "php://stdout", or "php://stderr",
Posted: Sun May 19, 2002 8:58 pm
by techniq
Thanks for replying. I've looked at sscanf() and see that it will read a string. How would I implement sscanf() and fopen() to do somthing llike this.
In other words can I duplicate this C++ code in PHP:
Code: Select all
void main() {
int age;
cout << "\nPlease enter your age: "
cin >> age;
if (age) {
cout << "\nYou are " << age << " years old!\n"
<< endl;
}
}
Thanks!
Posted: Sun May 19, 2002 9:43 pm
by volka
Code: Select all
$handle = fopen("php://stdin", "r");
fscanf($handle, "%ї0-9]", $age);
will read a number from stdin (I suppose - never done this

)
It works!!!
Posted: Sun May 19, 2002 10:47 pm
by techniq
Thanks for the sample! It works perfect!
Since you've never tried it, I've supplied a working example that can be used from the CLI. Thanks again!
Code: Select all
<?
// Retrieve age from user input
$handle = fopen("php://stdin", "r");
echo "\nPlease enter your age: ";
fscanf($handle, "%ї0-9]", &$age);
fclose($handle);
$age = $age + 10;
echo "\nYou will be $age years old in 10 years!\n";
?>