The method of obtaining the parameter mode php cli

php cli mode in two ways reception parameters

1. argv array
2. Method getopt


1. Use the argv array

For example: a need to implement php, and passes three parameters (type = news, is_hot = 1, limit = 5)

Create a test.php

<?php
print_r($argv);
?>


Execute the command line

php test.php news 1 5

Output:

Array
(
[0] => test.php
[1] => news
[2] => 1
[3] => 5
)

 



See argv [0] is the name of the file currently executing php, and argv [1] ~ argv [3 ] is the value of the parameter passed
argv [1] is equal to the value of the type
argv [2] is equal to the value is_hot
argv [ 3] is equal to the limit value
which can obtain the parameters passed to subsequent processing operations in accordance with the argv array.

Disadvantages:
Use argv array, the parameters can be passed sequentially acquired. However, after the acquisition, a need to make the corresponding process, the embodiment need argv [1], the corresponding type parameter, argv [2] corresponds is_hot parameters, argv [3] corresponding to limit parameters. If the delivery process, the parameter order wrong, it will cause an error.

E.g:

<?php
$param = array();
$param['type'] = $argv[1];
$param['is_hot'] = $argv[2];
$param['limit'] = $argv[3];

print_r($param);

 

carried out

php test.php news 1 5

输出:

Array
(
    [type] => news
    [is_hot] => 1
    [limit] => 5
)

 

Delivery order is different, the acquired parameter values ​​will be different, resulting in subsequent program error

carried out

php test.php 1 5 news

输出:

Array
(
    [type] => 1
    [is_hot] => 5
    [limit] => news
)

 


Therefore, when using the argv array transmission parameters, note the order of parameters passed.


2. Use getopt method


getopt Gets options from the command line parameter list

array getopt ( string $options [, array $longopts ] )


Parameters:
Options
string for each character will be treated as option character, matching options passed to the script of a single hyphen (-) at the beginning (). For example, an option string "x" recognizes an option -x. Allowing only az, AZ and 0-9.

longopts
array of options. Each element of this array will be used as option string, matched with two hyphens (-) passed to the script options. For example, the long option element "opt" recognizes an option -opt.

options may include the following elements:
individual characters (do not accept values)
followed by a colon character (this option requires a value)
followed by two characters colon (optional value for this option)
value of the option after the first string a parameter. It does not mind if there is a space before the value.

options and longopts format is almost the same, the only difference is longopts need is an array of options (each element as an option), while options require a string (each character is the option).
Separator may be passed by value or = spaces.
The value of the option is not accepted as a separator space, can only be used as a separator =.

The return value
of this function will return options / parameters, or FALSE on failure.
Parsing options will terminate in the first non-option found, anything after are discarded.


Example 1. options
a, b, c need value
d optional value
e is a value not accepted

<?php
$param = getopt('a:b:c:d::e');
print_r($param);


carried out

php test.php -a 1 -b 2 -c 3 -d=4 -e 5

输出:

Array
(
[a] => 1
[b] => 2
[c] => 3
[d] => 4
[e] => 
)

 

Example 2. longopts
type, is_hot need value
limit is selectable values
expire value is not accepted

<?php
$longopt = array(
'type:',
'is_hot:',
'limit::',
'expire'
);
$param = getopt('', $longopt);
print_r($param);

 

carried out

php test.php --type news --is_hot 1 --limit=10 --expire=100

输出:

Array
(
[type] => news
[is_hot] => 1
[limit] => 10
[expire] => 
)

 

3. Find the first non-option, behind ignore examples

<?php
$longopt = array(
'type:',
'is_hot:',
'limit::',
'expire'
);
$param = getopt('', $longopt);
print_r($param);


carried out

php test.php --type news --is_hots 1 --limit=10 --expire=100

输出:

Array
(
[type] => news
)

 


Because is_hots not an option value (defined is is_hot), so the argument after the start from here, have been dropped.


Summary:
Use pass argv array parameter method is simple and easy to implement. Order of the parameters can not be wrong, the parameter needs to be done to obtain the corresponding process.
Use getopt method, parameter name, parameter order may be free, fairly standard. (Recommended)

Guess you like

Origin www.cnblogs.com/ryanzheng/p/11761179.html