Geen cache-versie.

Caching uitgeschakeld. Standaardinstelling voor deze pagina:ingeschakeld (code LNG204)
Als het scherm te langzaam is, kunt u de gebruikersmodus uitschakelen om de cacheversie te bekijken.

Rechercher une fonction PHP

getopt

(PHP 4 >= 4.3.0, PHP 5, PHP 7)

getoptGets options from the command line argument list

Description

getopt ( string $options [, array $longopts [, int &$optind ]] ) : array

Parses options passed to the script.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

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.
optind
If the optind parameter is present, then the index where argument parsing stopped will be written to this variable.

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.

Note: Optional values do not accept " " (space) as a separator.

Note:

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).

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Return Values

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.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Changelog

Version Description
7.1.0 Added the optind parameter.
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.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Examples

Example #1 getopt() example: The basics

<?php
// Script example.php
$options getopt("f:hp:");
var_dump($options);
?>
shell> php example.php -fvalue -h

The above example will output:

array(2) {
  ["f"]=>
  string(5) "value"
  ["h"]=>
  bool(false)
}

Example #2 getopt() example: Introducing long options

<?php
// Script example.php
$shortopts  "";
$shortopts .= "f:";  // Required value
$shortopts .= "v::"// Optional value
$shortopts .= "abc"// These options do not accept values

$longopts  = array(
    
"required:",     // Required value
    
"optional::",    // Optional value
    
"option",        // No value
    
"opt",           // No value
);
$options getopt($shortopts$longopts);
var_dump($options);
?>
shell> php example.php -f "value for f" -v -a --required value --optional="optional value" --option

The above example will output:

array(6) {
  ["f"]=>
  string(11) "value for f"
  ["v"]=>
  bool(false)
  ["a"]=>
  bool(false)
  ["required"]=>
  string(5) "value"
  ["optional"]=>
  string(14) "optional value"
  ["option"]=>
  bool(false)
}

Example #3 getopt() example: Passing multiple options as one

<?php
// Script example.php
$options getopt("abc");
var_dump($options);
?>
shell> php example.php -aaac

The above example will output:

array(2) {
  ["a"]=>
  array(3) {
    [0]=>
    bool(false)
    [1]=>
    bool(false)
    [2]=>
    bool(false)
  }
  ["c"]=>
  bool(false)
}

Example #4 getopt() example: Using optind

<?php
// Script example.php
$optind null;
$opts getopt('a:b:', [], $optind);
$pos_args array_slice($argv$optind);
var_dump($pos_args);
shell> php example.php -a 1 -b 2 -- test

The above example will output:

array(1) {
  [0]=>
  string(4) "test"
}

Zoek een PHP-functie

Vertaling niet beschikbaar

De PHP-handleiding is nog niet in het Nederlands vertaald, dus het scherm is in het Engels. Als u wilt, kunt u het ook in het Frans of in het Duits raadplegen.

Als je de moed voelt, kun je je vertaling aanbieden ;-)

Nederlandse vertaling

U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.

Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.

Bij voorbaat dank.

Document heeft de 30/01/2003 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/php-rf-function.getopt.html

De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.

Referenties

  1. Bekijk - html-document Taal van het document:fr Manuel PHP : http://php.net

Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.

Inhoudsopgave Haut