Run the PHP script [with parameters] on the command line.

Source: Internet
Author: User
Tags php cli
This section describes how to execute the PHP script code in the command line. The actual purpose is to use php.exe. therefore, pay attention to setting environment variables. Create a simple text file that contains the following PHP code and save it as hello. php:

The code is as follows:


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

The code is as follows:


// 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

The code is as follows:


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

The code is as follows:


// 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, 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.

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.