<?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 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.eval.php',
    1 => 'eval',
    2 => '把字符串作为PHP代码执行',
  ),
  'up' => 
  array (
    0 => 'ref.misc.php',
    1 => '杂项 函数',
  ),
  'prev' => 
  array (
    0 => 'function.die.php',
    1 => 'die',
  ),
  'next' => 
  array (
    0 => 'function.exit.php',
    1 => 'exit',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    '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">把字符串作为PHP代码执行</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.eval-description">
  <h3 class="title">说明</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">
   把指定 <code class="parameter">code</code> 作为 PHP 代码执行。
  </p>
  <p class="para">
   正在执行的代码继承调用 <span class="function"><strong>eval()</strong></span> 所在行的<a href="language.variables.scope.php" class="link">变量作用域</a>。该行中任何有效变量都可在执行的代码中读取和修改。但定义的所有函数和类都将在全局命名空间中定义。换句话说，编译器将执行的代码视为单独
   <a href="function.include.php" class="link">included</a> 后的文件。
  </p>
  <div class="caution"><strong class="caution">警告</strong>
   <p class="para">
    <span class="function"><strong>eval()</strong></span> 语言构造<em>非常危险</em>，因为它允许执行任意 PHP
    代码。<em>因此不鼓励使用它。</em>如果已经仔细验证过除了使用此构造以外别无他法, 请多加注意不要在未事先正确验证的情况下<em>将任何用户提供的数据传递到</em>其中。
  </p>
  </div>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.eval-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">code</code></dt>
     <dd>
      <p class="para">
       需要被执行的字符串
      </p>
      <p class="para">
       代码不能包含打开/关闭 
       <a href="language.basic-syntax.phpmode.php" class="link">PHP tags</a>。比如，
       <code class="literal">&#039;echo &quot;Hi!&quot;;&#039;</code> 不能这样传入：
       <code class="literal">&#039;&lt;?php echo &quot;Hi!&quot;; ?&gt;&#039;</code>。但仍然可以用合适的 PHP tag 来离开、重新进入 PHP 模式。比如
       <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">
       除此之外，传入的必须是有效的 PHP 代码。所有的语句必须以分号结尾。比如 
       <code class="literal">&#039;echo &quot;Hi!&quot;&#039;</code> 会导致一个 parse error，而
       <code class="literal">&#039;echo &quot;Hi!&quot;;&#039;</code> 则会正常运行。
      </p>
      <p class="para">
       <code class="literal">return</code> 语句会立即中止当前字符串的执行。
      </p>
      <p class="para">
       代码执行的作用域是调用 <span class="function"><strong>eval()</strong></span> 处的作用域。因此，<span class="function"><strong>eval()</strong></span> 里任何的变量定义、修改，都会在函数结束后被保留。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.eval-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>eval()</strong></span> 返回 <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>，除非在执行的代码中 <code class="literal">return</code> 了一个值，函数返回传递给 <code class="literal">return</code> 的值。 PHP 7 开始，执行的代码里如果有一个 parse error，<span class="function"><strong>eval()</strong></span> 会抛出 <span class="classname"><a href="class.parseerror.php" class="classname">ParseError</a></span> 异常。在 PHP 7 之前，
   如果在执行的代码中有 parse error，<span class="function"><strong>eval()</strong></span> 返回
   <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>，之后的代码将正常执行。无法使用 <span class="function"><a href="function.set-error-handler.php" class="function">set_error_handler()</a></span> 捕获 <span class="function"><strong>eval()</strong></span> 中的解析错误。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.eval-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>eval()</strong></span> 例子 - 简单的文本合并</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>以上示例会输出：</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">注释</h3>

  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">因为是语言构造器而不是函数，不能被
<a href="functions.variable-functions.php" class="link">可变函数</a> 或者
<a href="functions.arguments.php#functions.named-arguments" class="link">命名参数</a> 调用。
</span>
</p></blockquote>

  <div class="tip"><strong class="tip">小技巧</strong><p class="simpara">和直接将结果输出到浏览器一样，可使用<a href="book.outcontrol.php" class="link">输出控制函数</a>来捕获当前函数的输出，然后(例如)保存到一个 <span class="type"><a href="language.types.string.php" class="type string">string</a></span> 中。</p></div>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    如果在执行的代码中产生了一个致命的错误（fatal error），整个脚本会退出。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.eval-seealso">
  <h3 class="title">参见</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> - 把第一个参数作为回调函数调用</span></li>
   </ul>
  </p>
 </div>

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