<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.exec.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.exec.php',
    1 => 'exec',
    2 => 'Execute an external program',
  ),
  'up' => 
  array (
    0 => 'ref.exec.php',
    1 => 'Program execution Functions',
  ),
  'prev' => 
  array (
    0 => 'function.escapeshellcmd.php',
    1 => 'escapeshellcmd',
  ),
  'next' => 
  array (
    0 => 'function.passthru.php',
    1 => 'passthru',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/exec/functions/exec.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.exec" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">exec</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">exec</span> &mdash; <span class="dc-title">Execute an external program</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.exec-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>exec</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$command</code></span>, <span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter reference">&$output</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter reference">&$result_code</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.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>exec()</strong></span> executes the given
   <code class="parameter">command</code>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.exec-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">command</code></dt>
     <dd>
      <p class="para">
       The command that will be executed.
      </p>
     </dd>
    
    
     <dt><code class="parameter">output</code></dt>
     <dd>
      <p class="para">
       If the <code class="parameter">output</code> argument is present, then the
       specified array will be filled with every line of output from the
       command.  Trailing whitespace, such as <code class="literal">\n</code>, is not
       included in this array.  Note that if the array already contains some
       elements, <span class="function"><strong>exec()</strong></span> will append to the end of the array.
       If you do not want the function to append elements, call
       <span class="function"><a href="function.unset.php" class="function">unset()</a></span> on the array before passing it to
       <span class="function"><strong>exec()</strong></span>.
      </p>
     </dd>
    
    
     <dt><code class="parameter">result_code</code></dt>
     <dd>
      <p class="para">
       If the <code class="parameter">result_code</code> argument is present
       along with the <code class="parameter">output</code> argument, then the
       return status of the executed command will be written to this
       variable.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.exec-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   The last line from the result of the command.  If you need to execute a 
   command and have all the data from the command passed directly back without 
   any interference, use the <span class="function"><a href="function.passthru.php" class="function">passthru()</a></span> function.
  </p>
  <p class="para">
   Returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure.
  </p>
  <p class="para">
   To get the output of the executed command, be sure to set and use the
   <code class="parameter">output</code> parameter.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.exec-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="para">
   Emits an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> if <span class="function"><strong>exec()</strong></span>
   is unable to execute the <code class="parameter">command</code>.
  </p>
  <p class="para">
   Throws a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> if <code class="parameter">command</code>
   is empty or contains null bytes.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.exec-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       If <code class="parameter">command</code> is empty or contains null bytes,
       <span class="function"><strong>exec()</strong></span> now throws a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span>.
       Previously it emitted an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> and returned <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.exec-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 An <span class="function"><strong>exec()</strong></span> example</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">// outputs the username that owns the running php/httpd process<br />// (on a system with the "whoami" executable in the path)<br /></span><span style="color: #0000BB">$output</span><span style="color: #007700">=</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$retval</span><span style="color: #007700">=</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">'whoami'</span><span style="color: #007700">, </span><span style="color: #0000BB">$output</span><span style="color: #007700">, </span><span style="color: #0000BB">$retval</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"Returned with status </span><span style="color: #0000BB">$retval</span><span style="color: #DD0000"> and output:\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$output</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output
something similar to:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Returned with status 0 and output:
Array
(
    [0] =&gt; cmb
)</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.exec-notes">
  <h3 class="title">Notes</h3>
  <div class="warning"><strong class="warning">Warning</strong><p class="para">When allowing user-supplied data to be
passed to this function, use
<span class="function"><a href="function.escapeshellarg.php" class="function">escapeshellarg()</a></span> or <span class="function"><a href="function.escapeshellcmd.php" class="function">escapeshellcmd()</a></span>
to ensure that users cannot trick the system into executing arbitrary
commands.</p></div>
  <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">If a program is started with this function,
in order for it to continue running in the background, the output of the
program must be redirected to a file or another output stream. Failing to do so
will cause PHP to hang until the execution of the program ends.</p></p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">On Windows <span class="function"><strong>exec()</strong></span>
will first start cmd.exe to launch the command. If you want to start an external program without starting cmd.exe
use <span class="function"><a href="function.proc-open.php" class="function">proc_open()</a></span> with the <code class="parameter">bypass_shell</code> option set.</p></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.exec-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.system.php" class="function" rel="rdfs-seeAlso">system()</a> - Execute an external program and display the output</span></li>
    <li><span class="function"><a href="function.passthru.php" class="function" rel="rdfs-seeAlso">passthru()</a> - Execute an external program and display raw output</span></li>
    <li><span class="function"><a href="function.escapeshellcmd.php" class="function" rel="rdfs-seeAlso">escapeshellcmd()</a> - Escape shell metacharacters</span></li>
    <li><span class="function"><a href="function.pcntl-exec.php" class="function" rel="rdfs-seeAlso">pcntl_exec()</a> - Executes specified program in current process space</span></li>
    <li><a href="language.operators.execution.php" class="link">backtick operator</a></li>
   </ul>
  </p>
 </div>

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