getopt()
getopt() — Gets options from the command line argument list.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
Signature
getopt(string $short_options, array $long_options = [], int &$rest_index = null): array|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $short_options | string | Each character in this string will be used as option characters and matched against options passed to the script starting with a single hyphen (-). |
| $long_options | array | 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 (--). |
| &$rest_index | int | If the rest_index parameter is present, then the index where argument parsing stopped will be written to this variable. |
Return value
This function will return an array of option / argument pairs, or false on failure. Note: The parsing of options will end at the first non-option found, anything that follows is discarded. The parsing of options will end at the first non-option found, anything that follows is discarded.
Example
A runnable snippet:
// Script example.php $options = getopt("f:hp:"); var_dump($options);
Output:
array(2) { ["f"]=> string(5) "value" ["h"]=> bool(false) }
Changelog
| Version | Change |
|---|---|
| 7.1.0 | Added the rest_index parameter. |
All PHP 8.5 built-in functions
Reference card built from the PHP manual page getopt() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.