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:
$name = trim(shell_exec("read -p 'Enter your name: ' name\necho \$name"));
echo "Hello $name this is PHP speaking\n";
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


  1. This comment has been removed by the author.

  2. This comment has been removed by the author.

  3. Thanks a lot for sharing us about this update. Hope you will not get tired on making posts as informative as this. 

    Java training in Chennai

    Java training in Bangalore

  4. After reading this web site I am very satisfied simply because this site is providing comprehensive knowledge for you to audience.
    Thank you to the perform as well as discuss anything incredibly important in my opinion. We loose time waiting for your next article writing in addition to I beg one to get back to pay a visit to our website in

    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

  5. Thanks For sharing the Information The Information Shared Is Very valuable please Keep Updating Us The InFormation Shared Is Very Valuable Python Online Training Hadoop Online Training <a href=">DataScience Online Training</a>


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,, 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.