Can PHP CLI perform cout/cin type Fuctionality?
Moderator: General Moderators
Can PHP CLI perform cout/cin type Fuctionality?
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.
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:
Thanks!
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;
}
}Code: Select all
$handle = fopen("php://stdin", "r");
fscanf($handle, "%ї0-9]", $age);It works!!!
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!
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";
?>