Home >Backend Development >PHP Tutorial >Detailed explanation of the usage of getopt in php

Detailed explanation of the usage of getopt in php

WBOY
WBOYOriginal
2016-07-25 08:58:521094browse
  1.  var_dump($argv);
  2. ?>
Copy code

Use command:

  1. php script.php arg1 arg2 arg3
Copy code

will output:

  1.  $options = getopt("f:hp:");
  2.  var_dump($options);
  3. ?>
Copy code
Copy the code Code example:
  1. Use the command:
  2. php script.php -f value -h or php script.php -fvalue -h
Copy the code

and the output will be:

  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. ?>< /p>
  14. php script.php -f "value for f" -v -a --required value --optional="optional value" --option will output:

Copy code

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 it will help everyone understand and master the usage of getopt in php. Programmer's Home, I wish you all learning and progress.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn