Php script (with parameters) running on the PHP command line

Source: Internet
Author: User
Tags php cli
Php script (with parameters) running on the PHP command line

  1. Echo "Hello from the CLI ";
  2. ?>

Now, try to run this program at the command line prompt by calling the CLI executable file and providing the Script File name: # php phphphello. php outputs Hello from the CLI

With standard input and output, you can use these three constants in your PHP script to accept user input or display the processing and computing results. To better understand this, you can refer to the following code.

  1. // Ask for input

  2. Fwrite (STDOUT, "Enter your name :");

  3. // Get input

  4. $ Name = trim (fgets (STDIN ));

  5. // Write input back

  6. Fwrite (STDOUT, "Hello, $ name! ");
  7. ?>

Look what happens when you run it: shell> php hello. phpEnter your name: JoeHello, Joe!?>

In this script, the fwrite () function first writes a message to the standard output device and asks the user's name. It then reads the user input information obtained from the standard input device into a PHP variable and merges it into a string. Then, use fwrite () to print the string to the standard output device.

# --- It is common to enter program parameters in the command line to change the running mode using the command line independent variables. You can also do this for the CLI program. Php cli has two special variables to achieve this purpose: one is the $ argv variable, which saves the parameters passed to the PHP script as separate array elements through the command line; the other is the $ argc variable, which is used to save the number of elements in the $ argv array.

It is very easy to write a piece of code that reads $ argv and processes the parameters it contains in a PHP script. Test the following sample script to see how it works:

  1. Print_r ($ argv );
  2. ?>

Run this script by passing it some arbitrary values, and check the output:

Shell> php phptest. php chocolate 276 "killer tie, dude! "Array ([0] => test. php [1] => chocolate [2] => 276 [3] => killer tie, dude !)

As you can see from the output, the value passed to test. php will automatically appear in $ argv as an array element. Note that the first independent variable of $ argvis is always the name of the script.

The following is a more complex example:

  1. // Check for all required arguments

  2. // First argument is always name of script!
  3. If ($ argc! = 4 ){
  4. Die ("Usage: book. php ");
  5. }

  6. // Remove first argument

  7. Array_shift ($ argv );

  8. // Get and use remaining arguments

  9. $ Checkin = $ argv [0];
  10. $ Nights = $ argv [1];
  11. $ Type = $ argv [2];
  12. Echo "You have requested a $ type room for $ nights, checking in on $ checkin. Thank you for your order! ";
  13. ?>

Example: shell> php phpbook. php 21/05/2005 7 singleYou have requested a single room for 7 nights, checking in on 21/05/2005. Thank you for your order!

Here, the script first checks $ argc to ensure that the number of independent variables meets the requirements. It then extracts each independent variable from $ argv and prints them to the standard output.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.