[Go]php CLI command custom parameter passing

Source: Internet
Author: User
Tags php cli zend

From:http://www.cnblogs.com/zcy_soft/archive/2011/12/10/2283437.html

All PHP distributions, whether compiled from source code or pre-created versions, are provided with a PHP executable by default. This executable can be used to run the command-line PHP program.
To find this executable file on your system, follow the steps below:
Windows: Placed in the main PHP installation directory, the file name is Php.exe or (in the old version of PHP) is Php-cli.exe.
Linux: Saved in the bin/subdirectory of the PHP installation directory.

It is important to note that the CLI mode and CGI mode runtime php.ini are not the same set of configurations and need to be configured separately.
Regardless of the operating system, you need to test it to make sure it works correctly by calling it with the-v parameter:

shell> /path/php.exe -vPHP 5.0.0 (cli) (built: Jun 1 2005 18:32:10)Copyright (c) 1997-2004 The PHP GroupZend Engine v2.0.0, Copyright (c) 1998-2004 Zend Technologies

It should return the version number of PHP.

Using CLI commands
A simple PHP CLI program, named hello.php

<?phpecho"Hello from the CLI";?>

Now, try running the program at a command-line prompt by invoking the CLI executable and providing the script's file name:

shell> /path/php.exe /example/hello.phpHello from the CLI

Use of standard inputs and outputs
The PHP CLI defines three constants to make it easier to interact with the interpreter at the command-line prompt. These constants are shown in the following table

Constant Description
Stdin Standard input Devices
STDOUT Standard output devices
STDERR Standard error Device




You can use these three constants in your PHP scripts to accept user input, or to display the results of processing and calculations.

Examples of Use:

<?php// ask for inputfwrite(STDOUT, "Enter your name: ");// get input$name = trim(fgets(STDIN));// write input backfwrite(STDOUT, "Hello, $name!");?>

Output

D:\>\wamp\bin\php\php5.3.0\php.exe  \tools\index.phpEnter your name: kkkHello, kkk!

In this script, the fwrite () function first writes a message to the standard output device asking for 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. The string is then printed out to the standard output device using fwrite ().

command Line custom variable 1 "$argv | $ARGC"
It is common practice to enter program parameters at the command line to change how they are run. You can also do this to the CLI program.
The PHP CLI has two special variables that are specifically designed to achieve this purpose:
One is $argv variable, which saves the parameters passed to the PHP script as a separate array element through the command line;
The other is $ARGC variable, which holds the number of elements in the $ARGV array.

Examples of Use:

<?phpprint_r($argv);?>

Output

D:\>\wamp\bin\php\php5.3.0\php.exe  \tools\index.php bac dddArray(    [0] => \tools\index.php    [1] => bac    [2] => ddd)

As you can see from the output, the value passed to the index.php is automatically shown as an array element in the $ARGV. Note that the first argument $argv is always the script's own name.

Note: We can also use console_getopt the Pear class adds more complex command-line arguments to PHP.

command line custom variable 2 "Receive parameters using console_getopt "

Note: This variable is only available when REGISTER_ARGC_ARGV is open

Getopt ($option, $longopts)///First $Option receives-H vb second parameter receives--require SSS

Usage examples

<?php        $shortopts= "";        $shortopts.= "f:"// Required value        $shortopts.= "v::"; // Optional value        $shortopts.= "abc"; // These options do not accept values        $longopts= array(            "required:", // Required value            "optional::", // Optional value            "option", // No value            "opt", // No value        );        $options= getopt($shortopts, $longopts);        var_dump($options);?>

Ouput:

D:\>\wamp\bin\php\php5.3.0\php.exe  \tools\index.php -f "value for f"-v -a --required value --optional="optional value" --option willarray(6) {  ["f"]=>  string(11) "value for f"  ["v"]=>  bool(false)  ["a"]=>  bool(false)  ["required"]=>  string(5) "value"  ["optional"]=>  string(14) "optional value"  ["option"]=>  bool(false)}

command-line variable 3 "using CLI parameters"

In addition to using the command line to pass PHP script parameters, you can also pass the PHP CLI parameter to change how it works.

Parameters Description
-A Running run interactively interactively
-C Path reads PHP's. ini file from path
-N Run directly without reading PHP's. ini file
-M List the compiled modules
-I. Display information about PHP builds
-L Check the syntax of the PHP script
-S Display the source code in a colored fashion
-W Display the source code after removing the comment
-H Show Help


Interactive mode
You can also interactively use the PHP CLI, which is the input command, to get results immediately.
To get this effect, you just need to invoke the CLI executable with one parameter, just like this:

shell> /path/to/php -aInteractive mode enabled<?phpechomktime();1121187283echo2+2;4exit();shell>

Alternatively, you can invoke the CLI executable without using the-a parameter and enter the complete script or code snippet directly.

Use <ctrl>-d to end the code snippet and let the CLI execute it. See the following example:

shell> /path/to/php<?phpechodate("d-M-Y h:i:s", time());?><Ctrl-D>12-Jul-2005 06:54:04

[Go]php CLI command custom parameter passing

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.