How to run a PHP script on the command line [with parameters]

Source: Internet
Author: User
Tags php cli
How to run a PHP script under the command line [with parameters]

Create a simple text file that contains the following PHP code and save it as hello.php:
echo "Hello from the CLI";
?>
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


-----------------use of standard inputs and outputs
? You can use these three constants in your PHP scripts to accept user input, or display the results of processing and calculations. To better understand this, take a look at the following script (

List A):

List A
Ask for input
Fwrite (STDOUT, "Enter Your Name:");

Get input
$name = Trim (fgets (STDIN));

Write input back
Fwrite (STDOUT, "Hello, $name!");
?>
Look how happens when you run it:
shell> PHP hello.php
Enter your Name:joe
Hello, 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

Takes a PHP variable and merges it into a string. The string is then printed out to the standard output device using fwrite ().


-----------------using command-line arguments
? It is common practice to enter program parameters at 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 specifically designed to achieve this

Objective:One is the $ARGV variable, which saves the arguments passed to the PHP script as separate array elements through the command line, and the other is the $ARGC variable, which is used to hold the elements of the $ARGV array.

Number.

??? It is very simple to write a piece of code that reads $ARGV and processes the parameters contained in a PHP script. Try the example script in list B to see how it works:

List B
Print_r ($ARGV);
?>

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. It is important to note that the first argument of a $argvis is always

The name of the script itself.


? Here is a more complex example (List C):

List C


Code

PHP
//? check?for?all?required?arguments
? first?argument?is?always?name?of?script!
if? ($ARGC?!=?4)? {?
die("usage:?book.php? ? ? ? ");
}

//? remove?first?argument
Array_shift($argv);

//? get?and?use?remaining?arguments
$checkin?=?$argv[0];
$nights?=?$argv[1];
$type?=?$argv[2];
Echo?"You?have?requested?a?$type? room?for?$nights? nights,?checking?in?on?$checkin.? Thank?you?for?your?order!?";
?>

?

??? Here is an example of its usage:

shell> PHP phpbook.php 21/05/2005 7 single
You are requested a single and 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.