Getop in PHP is used to receive cmd parameters.
For example, when you need PHP debugging in linxu, you often need parameter debugging.
Getopt is a command that can be passed in with parameters.
Usage:
Array
Getopt(String $ Options[, Array $ Longopts])
Note:
$ OptionsEach character in the string will be used as an option character and A hyphen (-) will be used to start matching with the script (-). For example, an option character "X" corresponds to an option-X. Only a-Z, a-Z, and 0-9 are allowed.
Spaces cannot be used as option characters.
Note:An array containing the parameters passed to the current script when running under the command line.
Note: This variable is only available when register_argc_argv is enabled.
Example 1: first understand the PHP Variables$ Argv
<?php var_dump($argv);?>
Run the following command: PHP script. php arg1 arg2 arg3:
array(4) { [0]=> string(10) "script.php" [1]=> string(4) "arg1" [2]=> string(4) "arg2" [3]=> string(4) "arg3"}
Example 2: getopt simple example
<?php $options = getopt("f:hp:"); var_dump($options);?>
Use the command: PHP script. php-F value-H or PHP script. php-fvalue-h to output the following content:
array(2) { ["f"]=> string(5) "value" ["h"]=> bool(false)}
Example 3: getopt
<?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);?>
PHP script. php-F "value for F"-v-a -- required value -- Optional = "Optional
Value "-- OptionWill output:
array(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)}