Home > Code Snippets, PHP > User input in php command line

User input in php command line

January 17th, 2007 Leave a comment Go to comments

Ever wondered how to capture the user input when writing php command line scripts ?

<?php
 
function getInput($msg){
  
fwrite(STDOUT"$msg: ");
  
$varin trim(fgets(STDIN));
  return 
$varin;
}
 
?>

The function above is being used by me in certain command line scripts, where I need user responses.

  1. ryan
    October 29th, 2011 at 00:18 | #1

    thank you..

  2. jc
    March 4th, 2012 at 08:33 | #2

    This is the kind of code that SHOULD be on the internet. Short, simple, and sweet. Every time I get code from the internet I have to sort out the overly complicated logic, rewrite it and shorten it. Not this time.

    Thanks!

  3. March 5th, 2012 at 07:26 | #3

    @jc
    Thanks JC, glad to know that these posts are useful for some one.

  4. karthikeyan
    March 12th, 2012 at 16:30 | #4

    without using html or xml how to get the inputs from the user

  5. hooga lala
    April 18th, 2012 at 15:11 | #5

    what crap is this???

  6. April 23rd, 2012 at 23:00 | #6

    This is command line php script and accepting input from the command line. Not much used, but we are using this kind in real big quantitiy even for administrative tasks, and further. Also we have an ldap suite which provides command line remote administration of the ldap servers, virtual box xml interface for controlling virtual box machines all accessible from very modest desktops which even do not have gui installed. See another command line php in action http://youtu.be/30wohPsWBa4

  7. April 23rd, 2012 at 23:00 | #7

    Do you think this is crap http://youtu.be/30wohPsWBa4

    This is an application which works directly on the Gnome Terminal, and uses Galileo XML Webservices, totally written in php and run using the php cli interpreter. Though this is not yet production ready, which is not a limitation of PHP, but of me with very poor knowledge in the Focal Point application, tried to simulate as much as I could. The video I agree is of poor presentation as I am very poor at it.

  8. drachia
    June 11th, 2012 at 16:35 | #8

    Thanks, this is very cool and quite useful indeed…

  9. Bob LaBlah
    July 31st, 2012 at 18:27 | #9

    @jc
    This is because your are incapable of understanding necessary complexity. Some things are hard, this thing isn’t.

  10. Mike
    September 6th, 2012 at 20:02 | #10

    The type of code I like to see :) Thank you very muchly

  1. No trackbacks yet.

thirty − twenty six =