Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 3216

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Gets options from the command line argument list</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.getmyuid.html">getmyuid</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.getrusage.html">getrusage</a></div>
 <div class="up"><a href="ref.info.html">PHP Options/Info Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><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)</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">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">array</span> <span class="methodname"><strong>getopt</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$options</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$longopts</code></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">Parameters</h3>
  <p class="para">
   <dl>

    <dt>

     <span class="term"><em><code class="parameter">options</code></em></span>
     <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 (<em>-</em>).
      </span>
      <span class="simpara">
       For example, an option string <em>&quot;x&quot;</em> recognizes an
       option <em>-x</em>.
      </span>
      <span class="simpara">
       Only a-z, A-Z and 0-9 are allowed.
      </span>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">longopts</code></em></span>
     <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 (<em>--</em>).
      </span>
      <span class="simpara">
       For example, an longopts element <em>&quot;opt&quot;</em> recognizes an
       option <em>--opt</em>.
      </span>
     </dd>

    </dt>

   </dl>

  </p>
  <p class="para">
   The <em><code class="parameter">options</code></em> parameter may contain the following
   elements:
   <ul class="simplelist">
    <li class="member">Individual characters (do not accept values)</li>
    <li class="member">Characters followed by a colon (parameter requires value)</li>
    <li class="member">Characters followed by two colons (optional value)</li>
   </ul>
   Option values are the first argument after the string. It does not matter
   if a value has leading white space or not.
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     Optional values do not accept <em>&quot; &quot;</em> (space) as a separator.
    </span>
   </p></blockquote>
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    The format for the <em><code class="parameter">options</code></em> and
    <em><code class="parameter">longopts</code></em> is almost the same, the only difference is
    that <em><code class="parameter">longopts</code></em> takes an array of options (where each
    element is the option) whereas <em><code class="parameter">options</code></em> 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">Return Values</h3>
  <p class="para">
   This function will return an array of option / argument pairs or <strong><code>FALSE</code></strong> on
   failure.
  </p>
  <blockquote class="note"><p><strong class="note">Note</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>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.0</td>
       <td>
        Added support for &quot;=&quot; as argument/value separator.
       </td>
      </tr>

      <tr>
       <td>5.3.0</td>
       <td>
        Added support for optional values (specified with &quot;::&quot;).
       </td>
      </tr>

      <tr>
       <td>5.3.0</td>
       <td>
        Parameter <em><code class="parameter">longopts</code></em> is available on all systems.
       </td>
      </tr>

      <tr>
       <td>5.3.0</td>
       <td>
        This function is no longer system dependent, and now works on Windows, too.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.getopt-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="getopt.examples-1">
    <p><strong>Example #1  <span class="function"><strong>getopt()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$options&nbsp;</span><span style="color: #007700">=&nbsp;</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"><p>
     Running the above script with <em>php script.php -fvalue -h</em>
     will output:
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
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>Example #2  <span class="function"><strong>getopt()</strong></span> example#2</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$shortopts&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$shortopts&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"f:"</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Required&nbsp;value<br /></span><span style="color: #0000BB">$shortopts&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"v::"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;Optional&nbsp;value<br /></span><span style="color: #0000BB">$shortopts&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"abc"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;These&nbsp;options&nbsp;do&nbsp;not&nbsp;accept&nbsp;values<br /><br /></span><span style="color: #0000BB">$longopts&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"required:"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Required&nbsp;value<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"optional::"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Optional&nbsp;value<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"option"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;No&nbsp;value<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"opt"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;No&nbsp;value<br /></span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">getopt</span><span style="color: #007700">(</span><span style="color: #0000BB">$shortopts</span><span style="color: #007700">,&nbsp;</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"><p>
     Running the above script with <em>php script.php -f &quot;value for f&quot; -v
     -a --required value --optional=&quot;optional value&quot; --option</em>
     will output:
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
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>Example #3  <span class="function"><strong>getopt()</strong></span> example#3</strong></p>
    <div class="example-contents"><p>Passing multiple options as one</p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$options&nbsp;</span><span style="color: #007700">=&nbsp;</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"><p>
     Running the above script with <em>php script.php -aaac</em>
     will output:
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
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>
 </div>


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


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.getmyuid.html">getmyuid</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.getrusage.html">getrusage</a></div>
 <div class="up"><a href="ref.info.html">PHP Options/Info Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>