PHP command line to run PHP script (with parameters) method

Source: Internet
Author: User
    1. echo "Hello from the CLI";
    2. ?>
Copy Code

Now, try running the program at a command-line prompt by invoking the CLI executable and providing the script's file name: #php phphello.php output Hello from the CLI

Using standard inputs and outputs you can use these three constants in your own PHP scripts to accept user input, or to display the results of processing and calculations. To understand this better, take a look at 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. ?>

Copy Code

Look how happens when do 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 asking for 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. The string is then printed out to the standard output device using fwrite ().

#---It is common practice to use command-line arguments to enter program parameters on the command line to change how they are run. You can also do this to the CLI program. The PHP CLI has two special variables for this purpose: one is the $ARGV variable, which saves the arguments passed to the PHP script as a separate array element through the command line, and the other is the $ARGC variable, which is used to hold the number of elements in the $ARGV array.

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

    1. Print_r ($ARGV);
    2. ?>
Copy Code

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 the test.php is automatically shown as an array element in the $ARGV. Note that the first argument $argvis is always the script's own name.

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 the $nights nights, checking in on $checkin. Thank for your order! ";
    13. ?>

Copy Code

The following is an example of its usage:shell> PHP phpbook.php 21/05/2005 7 Singleyou has requested a single hostel for 7 nights, checking in on 21/05 /2005. Thank for your order!

Here, the script first checks the $ARGC to ensure that the number of independent variables meets the requirements. It then extracts each of the arguments from the $argv and prints them out 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.