getoptGets options from the command line argument list
&reftitle.description;
arraygetoptstringoptionsarraylongopts
Parses options passed to the script.
&reftitle.parameters;
options
Each character in this string will be used as option characters and
matched against options passed to the script starting with a single
hyphen (-).
For example, an option string "x" recognizes an
option -x.
Only a-z, A-Z and 0-9 are allowed.
longopts
An array of options. Each element in this array will be used as option
strings and matched against options passed to the script starting with
two hyphens (--).
For example, an longopts element "opt" recognizes an
option --opt.
The options parameter may contain the following
elements:
Individual characters (do not accept values)Characters followed by a colon (parameter requires value)Characters followed by two colons (optional value)
Option values are the first argument after the string. If a value is required,
it does not matter whether the value has leading white space or not. See note.
Optional values do not accept " " (space) as a separator.
The format for the options and
longopts is almost the same, the only difference is
that longopts takes an array of options (where each
element is the option) whereas options takes a
string (where each character is the option).
&reftitle.returnvalues;
This function will return an array of option / argument pairs or &false; on
failure.
The parsing of options will end at the first non-option found, anything
that follows is discarded.
&reftitle.changelog;
&Version;&Description;5.3.0
Added support for "=" as argument/value separator.
5.3.0
Added support for optional values (specified with "::").
5.3.0
Parameter longopts is available on all systems.
5.3.0
This function is no longer system dependent, and now works on Windows, too.
&reftitle.examples;
getopt example: The basics
]]>
php example.php -fvalue -h
]]>
&example.outputs;
string(5) "value"
["h"]=>
bool(false)
}
]]>
getopt example: Introducing long options
]]>
php example.php -f "value for f" -v -a --required value --optional="optional value" --option
]]>
&example.outputs;
string(11) "value for f"
["v"]=>
bool(false)
["a"]=>
bool(false)
["required"]=>
string(5) "value"
["optional"]=>
string(14) "optional value"
["option"]=>
bool(false)
}
]]>
getopt example: Passing multiple options as one
]]>
php example.php -aaac
]]>
&example.outputs;
array(3) {
[0]=>
bool(false)
[1]=>
bool(false)
[2]=>
bool(false)
}
["c"]=>
bool(false)
}
]]>
&reftitle.seealso;
$argv