In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how PHP uses the getopt () function. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Getopt () gets options from the command line argument list
In fact, it is such a simple function that we can get the specified command value like the command option of Linux. And unlike $argv separated by spaces, the command option function encapsulates these command options into an array with the option name as the key and the equal sign as the value, which is more convenient for us to use.
/ / how does php get the PHP command line arguments .php-- aql-bang-2-cv-dv4-- efug 5 ccc ddd print_r (getopt ('a-v-v-d')); / / Array// (/ / [b] = > 2) / [c] = > 3) / [d] = > 4)
Isn't it amazing, and very intuitive, that we get the contents of b, c, d directly and the format is very clear in the form of key-value arrays. Some students want to ask, what about an and e? What about ccc and ddd in the back?
First of all, ccc and ddd are not standard option parameters, that is to say, this function receives options that start with -, so ccc and ddd will not be output here, and it should be noted that non-option parameters will interrupt the acquisition of option parameters, and options that start with-cannot be obtained if you continue to add-starting options after ccc, which we will see later. And-- what about the option parameters at the beginning? Let's look directly at the long options below.
The long option / / php how to get the PHP command line arguments .php-- aplom1-bread2-clock3-dong4-- efug5 ccc ddd print_r (getopt ('', ['a ccc ddd print_r', ['an Array// (/ / [a] = > 1p / [e] = > 5p /))
Yes, the second argument to the getopt () function defines this-- the beginning of the long option, and it's important to note that the first argument is of string type, and the second long option argument is of array type. Then we combine them, of course, we can get all the parameter information!
/ / how does php get the PHP command line arguments .php-- aplom1-bang 2-cput3-dv4-- eq5 ccc ddd print_r (getopt ('aRV bWR cWV dRV FRV, [' aRV dBV dVF') / / Array// (/ / [a] = > 1) / [b] = > 2) / [c] = > 3) / [d] = > 4 / [e] = > 5 /)
OK, is it okay to get parameter options? careful students must have found another problem: why should a colon be added after the option name defined in the parameter of the getopt () function? This is related to our colon rule, please read on.
Colon rule
The first two parameters of getopt () both support a set of rules for getting options:
Separate characters (do not accept values)
Characters followed by colons (this option requires a value)
Characters followed by two colons (optional value for this option)
Let's take a look at it directly through the code.
/ / one / / php how to get the PHP command line arguments .php-- aq1-bang 2-cations 3-dong4-- eBay 5 ccc ddd print_r (getopt ('abcdef')); / / Array// (/ / [b] = > / / [c] = > / / [d] = > / /) / / two / / php how to get the PHP command line arguments .php-fprint_r (getopt (' FRV')) / / Array// (/ / [f] = > / /) print_r (getopt ('FRV')); / / Array// (/ /) / 3 / / php how to get PHP command line arguments .php-f 22print_r (getopt ('FRV')); / Array// (/ / [f] = > / /) print_r (getopt ('FRV')) / / Array// (/ / [f] = > 22 Array// /) / / 4 / / php how to get the PHP command line arguments .php-f=22print_r (getopt ('FRV')); / / Array// (/ / [f] = > 22max /) print_r (getopt ('FRV')); / / Array// (/ / [f] = > 22max /)
This paragraph is relatively long, let's look at it together. The first is the abcdef without colons. The returned array contains keys but no values. Corresponding to the above rule, the values of these parameter options are not accepted. You pass these parameter options only with key names and the content is empty.
In the second paragraph, a parameter is defined, but no value is given. In this case, the double colon:: will have a key name, while the single colon: will have nothing.
The third paragraph is the option value in the form of a space, double colon:: there is a key name but no value, single colon: the key value is normal.
The fourth paragraph is the option value in the form of equal sign =, and both single and double colons receive the key value normally.
Option parameter interrupt
The problem of parameter interruptions mentioned above is that if a non-option parameter appears after the option parameter, getopt () will no longer be able to get everything after that non-option parameter.
/ / php how to get the PHP command line argument. The parsing of the php-fame 22 aa-baked 33 Universe / option terminates at the first non-option found, and anything after that is discarded. / / Array// (/ / [f] = > 22 Universe /)
Through this test, it is clear that the following b option is not available. At this point, if we want to know where the option parameter is or because of which parameter is interrupted, we can use the third argument of the getopt () function.
/ / how does php get the PHP command line argument .php-flip 22 aa-bang 33$ optind = null;getopt ('f _ argv _ b _ null;getopt, [], $optind); echo $optind, PHP_EOL; / / returns the index value of the interrupt location, 2echo $argv [$optind], PHP_EOL; / / equals the index order of $argv, aa
The comments are clearly written, and the third parameter calls back the index of the break position of the parameter option, and the index is in the same order as the index of $argv.
This is the end of the article on "how PHP uses the getopt () function". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.