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/

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete

About Me

My photo
I code. I figured I should start a blog that keeps track of the many questions and answers that are asked and answered along the way. The name of my blog is "One Q, One A". The name describes the format. When searching for an answer to a problem, I typically have to visit more than one site to get enough information to solve the issue at hand. I always end up on stackoverflow.com, quora.com, random blogs, etc before the answer is obtained. In my blog, each post will consist of one question and one answer. All the noise encountered along the way will be omitted.