Php cli introduction)

Source: Internet
Author: User
Tags php cli
Introduction to cli in php ()

All PHP distributions, whether compiled from source code or pre-created versions, contain a PHP executable file by default. This executable file can be used to run the PHP program of the command line.

To find this executable file on your system, follow these steps:

In the Windows operating system, the scripts are stored in the main PHP installation directory, and the file name is php.exe.exe (in the old PHP, php-cli.exe is used.

In Linux, it is stored in the bin/subdirectory of the PHP installation directory.

In any operating system, you need to test it to ensure it can run normally. the method is to call it with the-v parameter:

Shell>/path/to/php-v
PHP 5.0.0 (cli) (built: Jun 1 2005 18:32:10)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.0, Copyright (c) 1998-2004 Zend Technologies

It should return the PHP version number.

A simple php cli program
Once you find the CLI executable file, you can use it with a simple program. Create a simple text file that contains the following PHP code and save it as hello. 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:

Shell>/path/to/phphello. php
Hello from the CLI

Use standard input and output
Php cli defines three constants to make it easier to interact with the interpreter at the command line prompt. For the constants, see Table.

Table

Constant Description
STDIN Standard input device
STDOUT Standard output device
STDERR Standard error device

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
// 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>/path/to/phphello. 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. Try the sample 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>/path/to/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 the name of the script.

The following is a more complex example (list C ):

List C
// 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>/path/to/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 device.

Note: You can use the lele_getopt PEAR class to add more complex command line parameters to PHP.

Use CLI parameters
In addition to passing PHP script parameters through command lines, you can also pass php cli parameters to change the working mode. Table B lists some important parameters:

Table B

Parameters Description
- Interactive Run of Run interactively
-C Path: Read the php. ini file from path.
-N Run without reading the php. ini file.
-M List compiled modules
-I Display PHP build information
-L Check the syntax of the PHP script
-S Display source code in color
-W Displays the source code after the annotation is removed.
-H Show Help

Interaction Mode
You can also use php cli in interactive mode, that is, enter the command to get the result immediately. To achieve this effect, you only need to use a parameter to call the CLI executable file, as shown below:

Shell>/path/to/php-

You will see a blank line in which you can enter PHP code. Let's see:

Shell>/path/to/php-
Interactive mode enabled
Echo mktime ();
1121187283
Echo 2 + 2;
4
Exit ();
Shell>

Alternatively, you can call the CLI executable file without using the-a parameter and directly enter the complete script or code segment. Use -D to end the code segment and ask CLI to execute it. See the following example:

Shell>/path/to/php
Echo date ("d-M-Y h: I: s", time ());
?>

12-Jul-2005 06:54:04

This is the PHP command line. now you should have a good understanding of php cli and start to use it.

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.