Execute a php script with parameters under the command line

Source: Internet
Author: User
Tags php cli
Welcome to the Linux community forum and interact with 2 million technical staff to access the method for executing the php script with parameters under the command line: compile this executable file,

Welcome to the Linux community forum and interact with 2 million technical staff> go to the command line to execute a php script with parameters: php itself is a scripting language, however, we generally use apache to execute php. Of course, php can also be executed through command lines. similar to perl and other languages. the executable file php.exe is also used,

Welcome to the Linux community forum and interact with 2 million technicians>

To execute a php script with parameters in the command line:

Php itself is a scripting language, but we generally use apache to execute php. Of course, php can also be executed through command lines. It is similar to perl and other languages.

The main cause is to use the php.exe executable file, so you need to set the environment variable.

Let's take a look at the simplest code, phphello. php:

Echo "Hello php !";

?>

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 php!

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:

// Ask for input

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

// Get input

$ Name = trim (fgets (STDIN ));

// Write input back

Fwrite (STDOUT, "Hello, $ name !");

?>

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

Use command line Independent Variables

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 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 to see how it works:

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 result, it is passed to test. the php value automatically appears 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:

Code

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