PHP getopt Summary

Source: Internet
Author: User

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 "-- Option
Will 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)}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.