How can I run the PHP script on the CLI and pass parameters to the PHP script at the same time?

Source: Internet
Author: User
Tags php cli
How to run the PHP script in the command line [with parameters]
Create a simple text file that contains the following PHP code and save it as hello. php:
<? PHP
Echo "Hello from the CLI ";
?>
Now, try to run this program at the command line prompt by calling the CLI executable file and providing the script file name:
# PHP phphello. php
Output Hello from the CLI

----------------- Use Standard Input and Output
You can use these three constants in your PHP script to accept user input or display processing and computing results. To better understand this, you can look at the following script (

List ):

List
<? PHP
// Ask for input
Fwrite (stdout, "enter your name :");

// Get input
$ Name = trim (fgets (stdin ));

// Write input back
Fwrite (stdout, "Hello, $ name! ");
?>
Look what 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 and asks the user's name. Then it reads the user input information obtained from the standard input device.

Get a PHP variable and merge it into a string. Then, use fwrite () to print the string to the standard output device.

----------------- Use the command line independent variable
It is common to enter program parameters in the command line to change the running mode. You can also do this for the CLI program. Php cli has two special variables specifically used to achieve this

Objective: 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, it is used to save

Number.

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

List B
<? PHP
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 test. php will automatically appear in $ argv as an array element. Note that the first independent variable of $ argvis is always

Script Name.

The following 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 <check-in-date> <num-nights> <room-type> ");
}

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

The following is an example of its usage:

Shell> PHP phpbook. php 21/05/2005 7 single
You 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.

From: http://www.cnblogs.com/Lovepanda/archive/2010/01/19/1651897.html

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.