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

ParameterTypeWhat it takes
$short_optionsstringEach character in this string will be used as option characters and matched against options passed to the script starting with a single hyphen (-).
$long_optionsarrayAn 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_indexintIf 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

VersionChange
7.1.0Added 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.