getopt

FunctionParams and return types changed in PHP 8.0

Gets options from the command line argument list.

PHP 5
PHP 7
PHP 8.0
Improved
PHP 8.1
PHP 8.2-8.3
PHP 8.4
PHP 8.5
PHP 8.6

getopt Function synopsis

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

Parameters

$short_options

Typestring

$long_options

OptionalTypearrayDefault value[]

$rest_index

Passed by reference, OptionalDefault valuenull

Note that the parameter $rest_index is passed by reference, and contains additional/updated context data that may be useful.

Return value

Typearray|false

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.

Changes to the getopt Function

PHP 8.0

  • Return type added: array|false
  • Parameter name of parameter #1 changed: $options to $short_options
  • Parameter type added for parameter #1 ($short_options): string
  • Parameter name of parameter #2 changed: $opts to $long_options
  • Parameter type added for parameter #2 ($long_options): array
  • Parameter default value added for position #2 ($long_options): []
  • Parameter name of parameter #3 changed: $optind to $rest_index
  • Parameter default value added for position #3 ($rest_index): null
  getopt(
-     $options,
+     string $short_options,
-     $opts,
+     array $long_options = [],
-     &$optind
+     &$rest_index = null
-   )
+   ): array|false

PHP 7.1

  • Optional parameter added - #3: &$optind
-getopt($options, $opts)
+getopt(
+    $options,
+    $opts,
+    &$optind
+  )

getopt Function Availability

PHP VersionAvailability
PHP 8.6Upcoming Release Yes
PHP 8.5Supported (Latest) Yes
PHP 8.4Supported Yes
PHP 8.3Security-Fixes Only Yes
PHP 8.2Security-Fixes Only Yes
PHP 8.1Unsupported Yes
PHP 8.0Unsupported Yes
PHP 7.4Unsupported Yes
PHP 7.3Unsupported Yes
PHP 7.2Unsupported Yes
PHP 7.1Unsupported Yes
PHP 7.0Unsupported Yes
PHP 5.6Unsupported Yes
PHP 5.5Unsupported Yes
PHP 5.4Unsupported Yes
PHP 5.3Unsupported Yes