PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8
getopt - Gets options from the command line argument list
Code Examples

getopt( string$short_options, [array$long_options = []], [intrest_index = null] ): array|false

Parses options passed to the script.



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.


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.


If the rest_index parameter is present, then the index where argument parsing stopped will be written to this variable.

The short_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 long_options array values may contain: String (parameter does not accept any value) String followed by a colon (parameter requires value) String followed by two colons (optional value)


The format for the short_options and long_options is almost the same, the only difference is that long_options takes an array of options (where each element is the option) whereas short_options takes a string (where each character is the option).

Return Values

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.


Version Description
7.1.0 Added the rest_index parameter.

Example of getopt

Show all examples for getopt

PHP Version:

Function getopt:

PHP Options and Information Functions

Most used PHP functions