<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.misc.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.eval.php',
    1 => 'eval',
    2 => 'Evaluate a string as PHP code',
  ),
  'up' => 
  array (
    0 => 'ref.misc.php',
    1 => 'Misc. Functions',
  ),
  'prev' => 
  array (
    0 => 'function.die.php',
    1 => 'die',
  ),
  'next' => 
  array (
    0 => 'function.exit.php',
    1 => 'exit',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/misc/functions/eval.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.eval" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">eval</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">eval</span> &mdash; <span class="dc-title">Evaluate a string as PHP code</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.eval-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>eval</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$code</code></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   Evaluates the given <code class="parameter">code</code> as PHP.
  </p>
  <p class="para">
   The code being evaluated inherits the
   <a href="language.variables.scope.php" class="link">variable scope</a>
   of the line on which the <span class="function"><strong>eval()</strong></span> call occurs.
   Any variables available at that line will be available for reading and
   modification in the evaluated code.
   However, all functions and classes defined will be defined in the global namespace.
   In other words, the compiler considers the evaluated code as if it were a
   separate <a href="function.include.php" class="link">included</a> file.
  </p>
  <div class="caution"><strong class="caution">Caution</strong>
   <p class="para">
    The <span class="function"><strong>eval()</strong></span> language construct is <em>very dangerous</em>
    because it allows execution of arbitrary PHP code. <em>Its use thus is
    discouraged.</em> If you have carefully verified that there is no other option
    than to use this construct, pay special attention <em>not to pass any user
    provided data</em> into it without properly validating it beforehand.
   </p>
  </div>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.eval-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">code</code></dt>
     <dd>
      <p class="para">
       Valid PHP code to be evaluated.
      </p>
      <p class="para">
       The code must not be wrapped in opening and closing
       <a href="language.basic-syntax.phpmode.php" class="link">PHP tags</a>, i.e.
       <code class="literal">&#039;echo &quot;Hi!&quot;;&#039;</code> must be passed instead of
       <code class="literal">&#039;&lt;?php echo &quot;Hi!&quot;; ?&gt;&#039;</code>. It is still possible to leave and
       re-enter PHP mode though using the appropriate PHP tags, e.g.
       <code class="literal">&#039;echo &quot;In PHP mode!&quot;; ?&gt;In HTML mode!&lt;?php echo &quot;Back in PHP mode!&quot;;&#039;</code>.
      </p>
      <p class="para">
       Apart from that the passed code must be valid PHP. This includes that all statements
       must be properly terminated using a semicolon.
       <code class="literal">&#039;echo &quot;Hi!&quot;&#039;</code> for example will cause a parse error, whereas
       <code class="literal">&#039;echo &quot;Hi!&quot;;&#039;</code> will work.
      </p>
      <p class="para">
       A <code class="literal">return</code> statement will immediately terminate the
       evaluation of the code. 
      </p>
      <p class="para">
       The code will be executed in the scope of the code calling <span class="function"><strong>eval()</strong></span>. Thus any
       variables defined or changed in the <span class="function"><strong>eval()</strong></span> call will remain visible after
       it terminates.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.eval-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   <span class="function"><strong>eval()</strong></span> returns <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> unless 
   <code class="literal">return</code> is called in the evaluated code, in which case
   the value passed to <code class="literal">return</code> is returned. As of PHP 7, if there is a
   parse error in the evaluated code, <span class="function"><strong>eval()</strong></span> throws a <span class="classname"><a href="class.parseerror.php" class="classname">ParseError</a></span> exception.
   Before PHP 7, in this case <span class="function"><strong>eval()</strong></span> returned
   <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> and execution of the following code continued normally. It is
   not possible to catch a parse error in <span class="function"><strong>eval()</strong></span>
   using <span class="function"><a href="function.set-error-handler.php" class="function">set_error_handler()</a></span>.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.eval-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>eval()</strong></span> example - simple text merge</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$string </span><span style="color: #007700">= </span><span style="color: #DD0000">'cup'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$name </span><span style="color: #007700">= </span><span style="color: #DD0000">'coffee'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'This is a $string with my $name in it.'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$str</span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />eval(</span><span style="color: #DD0000">"\$str = \"</span><span style="color: #0000BB">$str</span><span style="color: #DD0000">\";"</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$str</span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</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:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">This is a $string with my $name in it.
This is a cup with my coffee in it.</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.eval-notes">
  <h3 class="title">Notes</h3>

  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">Because this is a
language construct and not a function, it cannot be called using
<a href="functions.variable-functions.php" class="link">variable functions</a>,
or <a href="functions.arguments.php#functions.named-arguments" class="link">named arguments</a>.</span>
</p></blockquote>

  <div class="tip"><strong class="tip">Tip</strong><p class="simpara">As with anything that outputs
its result directly to the browser, the <a href="book.outcontrol.php" class="link">output-control functions</a> can be used to capture
the output of this function, and save it in a
<span class="type"><a href="language.types.string.php" class="type string">string</a></span> (for example).</p></div>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    In case of a fatal error in the evaluated code, the whole script exits.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.eval-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.call-user-func.php" class="function" rel="rdfs-seeAlso">call_user_func()</a> - Call the callback given by the first parameter</span></li>
   </ul>
  </p>
 </div>

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