PHP receives parameters in CLI mode in two ways
1. Using the argv array
2. Using the Getopt method
1. Using the argv array
For example: Need to execute a PHP and pass three parameters (Type=news, is_hot=1, limit=5)
Create test.php
<?phpprint_r ($ARGV);? >
Executing at the command line
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 executing PHP file, while argv[1]~argv[3] is the value of the passed parameter
Argv[1] equals the value of type
ARGV[2] equals the value of Is_hot
Argv[3] equals the value of limit
This allows for subsequent processing of the passed parameters according to the argv array.
Disadvantages:
Using the argv array, you can get the passed arguments in order. But after obtaining, need to do a correspondence processing, the above example needs to be argv[1] corresponding to the type parameter, argv[2] corresponding to the Is_hot parameter, argv[3] corresponding to the limit parameter. If, in the process of passing, the parameter is incorrectly written, it causes the program to fail.
For example:
<?php$param = Array (); $param [' type '] = $argv [1]; $param [' is_hot '] = $argv [2]; $param [' limit '] = $argv [3];p Rint_r ($ param);? >
Perform
PHP test.php News 1 5
Output:
Array ( [Type] = News [Is_hot] = 1 [limit] + 5)
When the order of delivery is different, the obtained parameter values will be different, resulting in a subsequent program error
Perform
PHP test.php 1 5 News
Output:
Array ( [Type] = 1 [is_hot] + 5 [limit] = News)
therefore, when passing parameters using the argv array, you need to be aware of the order in which the parameters are passed.
2. Using the Getopt method
Getopt getting options from the command-line argument list
Array getopt (string $options [, array $longopts])
Parameters:
Options
Each character in the string is treated as an option character, and the option to match the incoming script begins with a single hyphen (-). For example, an option string "X" recognizes an option-X. Only A-Z, A-Z, and 0-9 are allowed.
longopts
An array of options. Each element in this array is treated as an option string, matching the option to pass in a script with two hyphens (–). For example, the long option element "opt" identifies an option –opt.
The options may contain the following elements:
Individual characters (values not accepted)
Character followed by a colon (this option requires a value)
Characters followed by two colons (the value of this option is optional)
The value of the option is the first argument after a string. It doesn't mind if there are spaces before the value.
The options and longopts formats are almost identical, the only difference being that longopts needs to be an array of options (each element is an option), and the options need a string (each character is an option).
The pass-through delimiter can use spaces or =.
Optional values do not accept spaces as separators, only = are used as separators.
return value
This function returns the option/parameter pair, which returns FALSE on failure.
The resolution of the option terminates at the first non-option found, after which everything is discarded.
1. Using the options instance
A,b,c is a required value
D is an optional value
E is not an acceptable value
<?php$param = getopt (' a:b:c:d::e ');p rint_r ($param);? >
Perform
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. Using Longopts instances
Type,is_hot is a required value
Limit is an optional value
Expire is not an acceptable value
<?php$longopt = Array ( ' type: ', ' is_hot: ', ' limit:: ', ' expire '); $param = Getopt (', $longopt); Print_r ($param);? >
Perform
PHP test.php--type News--is_hot 1--limit=10--expire=100
Output:
Array ( [type] + news [Is_hot] = 1 [limit] = [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);? >
Perform
PHP test.php--type News--is_hots 1--limit=10--expire=100
Output:
Array ( [Type] = News)
Since is_hots is not an option value (it is defined as Is_hot), the arguments from here onwards are discarded.
Summary:
Using argv array to pass parameters, the method is simple and easy to implement. The order of the parameters can not be wrong, the parameters obtained after the corresponding processing.
Using the Getopt method, the parameter names can be used, and the order of the parameters can be arbitrary and comparative specification. (recommended)
This article introduces the PHP CLI mode to obtain the parameters of the method, more relevant content please focus on the PHP Chinese web.
Related recommendations:
How to recursively get the value of a specified key in an array by using PHP in code
Read a 1G file size with PHP implementation
Explain PHP class initialization function code