Method for obtaining parameters in php cli mode, phpcli
PHP can receive parameters in cli mode in two ways.
1. Use an argv Array
2. Use the getopt Method
1. Use an argv Array
For example, you need to execute a php program and pass three parameters (type = news, is_hot = 1, limit = 5)
Create test. php
<?phpprint_r($argv);?>
Execute
php test.php news 1 5
Output:
Array( [0] => test.php [1] => news [2] => 1 [3] => 5)
You can see that argv [0] is the name of the currently executed PHP file, while argv [1] ~ Argv [3] indicates the value of the passed parameter.
Argv [1] is equal to the value of type
Argv [2] is equal to the value of is_hot
Argv [3] is equal to the limit value.
In this way, you can obtain the passed parameters based on the argv array for subsequent processing.
Disadvantages:
You can use the argv array to obtain the passed parameters in sequence. However, a corresponding processing is required. In the above example, the argv [1] corresponds to the type parameter, argv [2] corresponds to the is_hot parameter, and argv [3] corresponds to the limit parameter. If the Parameter order is incorrect during the transfer process, the program may fail.
For example:
<?php$param = array();$param['type'] = $argv[1];$param['is_hot'] = $argv[2];$param['limit'] = $argv[3];print_r($param);?>
Run
php test.php news 1 5
Output:
Array( [type] => news [is_hot] => 1 [limit] => 5)
The transfer order is different, and the obtained parameter values are different, resulting in subsequent program errors.
Run
php test.php 1 5 news
Output:
Array( [type] => 1 [is_hot] => 5 [limit] => news)
Therefore, when passing parameters using the argv array, you must note the order in which the parameters are transmitted.
2. Use the getopt Method
Getopt obtains the option from the command line parameter list.
array getopt ( string $options [, array $longopts ] )
Parameters:
Options
Each character in the string is treated as an option character. The option that matches the input script starts with a single hyphen. For example, an option string "x" identifies an option-x. Only a-z, A-Z, and 0-9 are allowed.
Longopts
Option array. Each element in this array is used as an option string and matches the options passed into the script with two hyphens. For example, the long option element "opt" recognizes an option-opt.
Options may contain the following elements:
Separate characters (not allowed)
Followed by a colon (this option requires a value)
Followed by two colons (optional)
The value of the option is the first parameter after the string. It does not mind whether there is a space before the value.
The formats of options and longopts are almost the same. The only difference is that longopts needs to be an array of options (each element is an option ), options requires a string (each character is an option ).
You can use spaces or = to separate values.
Optional values do not accept spaces as separators and can only use = as separators.
Return Value
This function returns an option/parameter pair, and FALSE if it fails.
The parsing of options will end with the first non-option found, and any subsequent items will be discarded.
1. Use the options instance
A, B, and c are required values.
D is an optional value.
E is an unacceptable value.
<?php$param = getopt('a:b:c:d::e');print_r($param);?>
Run
php test.php -a 1 -b 2 -c 3 -d=4 -e 5
Output:
Array( [a] => 1 [b] => 2 [c] => 3 [d] => 4 [e] => )
2. Use longopts instances
Type, is_hot is the required value
Limit is an optional value.
Expire is an unacceptable value.
<?php$longopt = array( 'type:', 'is_hot:', 'limit::', 'expire');$param = getopt('', $longopt);print_r($param);?>
Run
php test.php --type news --is_hot 1 --limit=10 --expire=100
Output:
Array( [type] => news [is_hot] => 1 [limit] => 10 [expire] => )
3. Find the first non-option and ignore the instance later.
<?php$longopt = array( 'type:', 'is_hot:', 'limit::', 'expire');$param = getopt('', $longopt);print_r($param);?>
Run
php test.php --type news --is_hots 1 --limit=10 --expire=100
Output:
Array( [type] => news)
Because is_hots is not an option value (is_hot is defined), all parameters after this are discarded.
Summary:
Using argv array to pass parameters is simple and easy to implement. The Parameter order cannot be incorrect. corresponding processing is required after the parameter is obtained.
Using the getopt method, you can use the parameter name. The Parameter order is optional and the parameter is more standard. (Recommended)
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!