eHow Blog:

User-Submitted Article

How to Prompt the User in a Command Line Interface Using PHP

Prompt the User in a Command Line Interface Using PHP
Prompt the User in a Command Line Interface Using PHP

I have been starting to put up some basics of PHP using a command line interface(CLI). A very useful tool when using a CLI is to be able to prompt the user for input data, and to respond to the user using whatever input data they may have entered in to the CLI. I am going to show you a brief example of how you can use PHP to write a short script to prompt the user through a CLI.

Difficulty: Moderately Easy
Instructions
  1. Step 1

    Being able to prompt the user in a command line interface(CLI) using PHP is a very useful tool and I have enjoyed using it in my own programming tasks. Open up your script editor and your command line interface and get ready to try it for yourself!

  2. Step 2

    I will give you an example of my short code example. In this example, I am using fwrite and STDOUT to output a string to the command line interface. Here is the first line of code.

    fwrite(STDOUT, "Hello...\nWhat is your name? ");

  3. Step 3

    Now it is time for the user to input something into the CLI. The code for this will be using fgets and STDIN to recieve the input data. I am also using the trim function to get rid of any excess whitespace around the input data, just in case. I have set a variable, "$name", to represent the input data, so that I can call on this variable when I need it. Our code now looks like this.

    fwrite(STDOUT, "Hello...\nWhat is your name? ");
    $name = trim(fgets(STDIN));

  4. Step 4
    Entire PHP Script
     
    Entire PHP Script

    The last step of the code is using the data that we will input! This is a simple example so all we are going to do is have the program say something back using the name that we will enter into the CLI. The final line of code will again be using fwrite and STDOUT to output a string to the CLI, but this time, that string will contain our "$name" variable. The final code looks like this. I also included an image of the code in my script editor.

    fwrite(STDOUT, "Hello...\nWhat is your name? ");
    $name = trim(fgets(STDIN));
    fwrite(STDOUT, "Hello, $name!\n");

  5. Step 5

    Now we will run the code, and the following will take place... The program will output,
    "Hello..."
    "What is your name?"

    This is the part where you input your data, or name in this case. I will enter the name Bob, and press enter to continue the script. Our variable "$name" is now equal to our input data, "Bob". The program will now output a response.
    "Hello, Bob!"

  6. Step 6
    Output of Command Line Interface
     
    Output of Command Line Interface

    I hope that this helps you in your own script writing. I have found prompting the user to be a very helpful and convenient. I will add an image to this step of the entire output of the program in my command line interface. Enjoy!

Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This

Computers Fans

Follow us

  • Computers
  • Computers
Get Free Computers Newsletters
eHow At Home
eHow At Home

Copyright © 1999-2010 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy .   en-US † requires javascript

eHow Computers
eHow_eHow Technology and Electronics