Go deep into the usage of getopt in php

Source: Internet
Author: User
Go deep into the usage of getopt in php

  1. Var_dump ($ argv );
  2. ?>

Run the following command:

  1. Php script. php arg1 arg2 arg3

The output is as follows:

  1. $ Options = getopt ("f: hp :");
  2. Var_dump ($ options );
  3. ?>

Sample code:

  1. Run the following command:
  2. Php script. php-f value-h or php script. php-fvalue-h

The output is as follows:

  1. $ Shortopts = "";

  2. $ Shortopts. = "f:"; // Required value
  3. $ Shortopts. = "v:"; // Optional value
  4. $ Shortopts. = "abc"; // These options do not accept values

  5. $ Longopts = array (

  6. "Required:", // Required value
  7. "Optional:", // Optional value
  8. "Option", // No value
  9. "Opt", // No value
  10. );
  11. $ Options = getopt ($ shortopts, $ longopts );
  12. Var_dump ($ options );
  13. ?>

  14. 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.

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.