<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.info.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'de',
  ),
  'this' => 
  array (
    0 => 'function.getopt.php',
    1 => 'getopt',
    2 => 'Gets options from the command line argument list',
  ),
  'up' => 
  array (
    0 => 'ref.info.php',
    1 => 'PHP-Optionen-/-Informationen-Funktionen',
  ),
  'prev' => 
  array (
    0 => 'function.getmyuid.php',
    1 => 'getmyuid',
  ),
  'next' => 
  array (
    0 => 'function.getrusage.php',
    1 => 'getrusage',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/info/functions/getopt.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.getopt" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">getopt</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">getopt</span> &mdash; <span class="dc-title">Gets options from the command line argument list</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.getopt-description">
  <h3 class="title">Beschreibung</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>getopt</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$short_options</code></span>, <span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$long_options</code><span class="initializer"> = []</span></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter reference">&$rest_index</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   Parses options passed to the script.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.getopt-parameters">
  <h3 class="title">Parameter-Liste</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">short_options</code></dt>
     <dd>
      <span class="simpara">
       Each character in this string will be used as option characters and
       matched against options passed to the script starting with a single
       hyphen (<code class="literal">-</code>).
      </span>
      <span class="simpara">
       For example, an option string <code class="literal">&quot;x&quot;</code> recognizes an
       option <code class="literal">-x</code>.
      </span>
      <span class="simpara">
       Only a-z, A-Z and 0-9 are allowed.
      </span>
     </dd>
    
    
     <dt><code class="parameter">long_options</code></dt>
     <dd>
      <span class="simpara">
       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 (<code class="literal">--</code>).
      </span>
      <span class="simpara">
       For example, an longopts element <code class="literal">&quot;opt&quot;</code> recognizes an
       option <code class="literal">--opt</code>.
      </span>
     </dd>
    
    
     <dt><code class="parameter">rest_index</code></dt>
     <dd>
      <span class="simpara">
       If the <code class="parameter">rest_index</code> parameter is present, then the
       index where argument parsing stopped will be written to this variable.
      </span>
     </dd>
    
   </dl>
  </p>
  <p class="para">
   The <code class="parameter">short_options</code> parameter may contain the following
   elements:
   <ul class="simplelist">
    <li>Individual characters (do not accept values)</li>
    <li>Characters followed by a colon (parameter requires value)</li>
    <li>Characters followed by two colons (optional value)</li>
   </ul>
   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.
   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <span class="simpara">
     Optional values do not accept <code class="literal">&quot; &quot;</code> (space) as a separator.
    </span>
   </p></blockquote>
  </p>
  <p class="para">
    The <code class="parameter">long_options</code> array values may contain:
    <ul class="simplelist">
      <li>String (parameter does not accept any value)</li>
      <li>String followed by a colon (parameter requires value)</li>
      <li>String followed by two colons (optional value)</li>
    </ul>
  </p>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    The format for the <code class="parameter">short_options</code> and
    <code class="parameter">long_options</code> is almost the same, the only difference is
    that <code class="parameter">long_options</code> takes an array of options (where each
    element is the option) whereas <code class="parameter">short_options</code> takes a
    string (where each character is the option).
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.getopt-returnvalues">
  <h3 class="title">Rückgabewerte</h3>
  <p class="para">
   This function will return an array of option / argument pairs, Bei einem Fehler wird <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> zurückgegeben..
  </p>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    The parsing of options will end at the first non-option found, anything
    that follows is discarded.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.getopt-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Beschreibung</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.1.0</td>
       <td>
        Added the <code class="parameter">rest_index</code> parameter.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.getopt-examples">
  <h3 class="title">Beispiele</h3>
  <p class="para">
   <div class="example" id="getopt.examples-1">
    <p><strong>Beispiel #1 <span class="function"><strong>getopt()</strong></span> example: The basics</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Script example.php<br /></span><span style="color: #0000BB">$options </span><span style="color: #007700">= </span><span style="color: #0000BB">getopt</span><span style="color: #007700">(</span><span style="color: #DD0000">"f:hp:"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents">
<div class="shellcode"><pre class="shellcode">shell&gt; php example.php -fvalue -h</pre>
</div>
    </div>

    <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">array(2) {
  [&quot;f&quot;]=&gt;
  string(5) &quot;value&quot;
  [&quot;h&quot;]=&gt;
  bool(false)
}</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="getopt.examples-2">
    <p><strong>Beispiel #2 <span class="function"><strong>getopt()</strong></span> example: Introducing long options</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Script example.php<br /></span><span style="color: #0000BB">$shortopts  </span><span style="color: #007700">= </span><span style="color: #DD0000">""</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$shortopts </span><span style="color: #007700">.= </span><span style="color: #DD0000">"f:"</span><span style="color: #007700">;  </span><span style="color: #FF8000">// Required value<br /></span><span style="color: #0000BB">$shortopts </span><span style="color: #007700">.= </span><span style="color: #DD0000">"v::"</span><span style="color: #007700">; </span><span style="color: #FF8000">// Optional value<br /></span><span style="color: #0000BB">$shortopts </span><span style="color: #007700">.= </span><span style="color: #DD0000">"abc"</span><span style="color: #007700">; </span><span style="color: #FF8000">// These options do not accept values<br /><br /></span><span style="color: #0000BB">$longopts  </span><span style="color: #007700">= array(<br />    </span><span style="color: #DD0000">"required:"</span><span style="color: #007700">,     </span><span style="color: #FF8000">// Required value<br />    </span><span style="color: #DD0000">"optional::"</span><span style="color: #007700">,    </span><span style="color: #FF8000">// Optional value<br />    </span><span style="color: #DD0000">"option"</span><span style="color: #007700">,        </span><span style="color: #FF8000">// No value<br />    </span><span style="color: #DD0000">"opt"</span><span style="color: #007700">,           </span><span style="color: #FF8000">// No value<br /></span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$options </span><span style="color: #007700">= </span><span style="color: #0000BB">getopt</span><span style="color: #007700">(</span><span style="color: #0000BB">$shortopts</span><span style="color: #007700">, </span><span style="color: #0000BB">$longopts</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents">
<div class="shellcode"><pre class="shellcode">shell&gt; php example.php -f &quot;value for f&quot; -v -a --required value --optional=&quot;optional value&quot; --option</pre>
</div>
    </div>

    <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">array(6) {
  [&quot;f&quot;]=&gt;
  string(11) &quot;value for f&quot;
  [&quot;v&quot;]=&gt;
  bool(false)
  [&quot;a&quot;]=&gt;
  bool(false)
  [&quot;required&quot;]=&gt;
  string(5) &quot;value&quot;
  [&quot;optional&quot;]=&gt;
  string(14) &quot;optional value&quot;
  [&quot;option&quot;]=&gt;
  bool(false)
}</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="getopt.examples-3">
    <p><strong>Beispiel #3 <span class="function"><strong>getopt()</strong></span> example: Passing multiple options as one</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Script example.php<br /></span><span style="color: #0000BB">$options </span><span style="color: #007700">= </span><span style="color: #0000BB">getopt</span><span style="color: #007700">(</span><span style="color: #DD0000">"abc"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents">
<div class="shellcode"><pre class="shellcode">shell&gt; php example.php -aaac</pre>
</div>
    </div>

    <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">array(2) {
  [&quot;a&quot;]=&gt;
  array(3) {
    [0]=&gt;
    bool(false)
    [1]=&gt;
    bool(false)
    [2]=&gt;
    bool(false)
  }
  [&quot;c&quot;]=&gt;
  bool(false)
}</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="getopt.examples-4">
    <p><strong>Beispiel #4 <span class="function"><strong>getopt()</strong></span> example: Using <code class="parameter">rest_index</code></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Script example.php<br /></span><span style="color: #0000BB">$rest_index </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$opts </span><span style="color: #007700">= </span><span style="color: #0000BB">getopt</span><span style="color: #007700">(</span><span style="color: #DD0000">'a:b:'</span><span style="color: #007700">, [], </span><span style="color: #0000BB">$rest_index</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$pos_args </span><span style="color: #007700">= </span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$argv</span><span style="color: #007700">, </span><span style="color: #0000BB">$rest_index</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$pos_args</span><span style="color: #007700">);</span></span></code></div>
    </div>

    <div class="example-contents">
<div class="shellcode"><pre class="shellcode">shell&gt; php example.php -a 1 -b 2 -- test</pre>
</div>
    </div>

    <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">array(1) {
  [0]=&gt;
  string(4) &quot;test&quot;
}</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.getopt-seealso">
  <h3 class="title">Siehe auch</h3>
  <p class="para">
   <ul class="simplelist">
    <li><a href="reserved.variables.argv.php" class="link"><var class="varname"><a href="reserved.variables.argv.php" class="classname">$argv</a></var></a></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>