Monday, April 30, 2012

How can I capture user input from the cmd line using PHP?

Copy and paste the following command into your shell to create a new file in your current directory:
vim example.php
Copy and paste the following text into your new example.php file:
<?php
$name = trim(shell_exec("read -p 'Enter your name: ' name\necho \$name"));
echo "Hello $name this is PHP speaking\n";
exit;
Save the example.php file.

Next, experience your new PHP program (example.php) capture your user input from the cmd line by copying and pasting the following command into your shell:
php example.php
It will prompt you with the following:
Enter your name: 
You will type your name and then hit enter as shown here:
Enter your name: John
After you hit enter you'll see the PHP program we wrote print out the following text:
Hello John this is PHP speaking
Looking for a cloud-based catalog and CRM platform? Checkout http://rocware.com/

1 comment: