Go deep into the usage of getopt in php
- Var_dump ($ argv );
- ?>
Run the following command:
- Php script. php arg1 arg2 arg3
-
The output is as follows:
- $ Options = getopt ("f: hp :");
- Var_dump ($ options );
- ?>
Sample code:
- Run the following command:
- Php script. php-f value-h or php script. php-fvalue-h
The output is as follows:
$ 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" -- option will output:
-
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 )}With the above introduction, I hope to help you understand and master the usage of getopt in php, programmer's house, and wish you a better learning experience. |