Use PHP command line control script

Source: Internet
Author: User
Tags echo date php cli

Executable files

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:

<? 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
<? 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>/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
<? PHP
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
<? 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>/N ");
}

// 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! /N ";
?>

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 useLele_getoptThe pear class adds 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
<? PHP
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 <Ctrl>-D to end the code snippet and ask CLI to execute it. See the following example:

Shell>/path/to/PHP
<? PHP
Echo date ("D-M-Y H: I: s", time ());
?>
<Ctrl-D>
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.

 

Related Article

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.