1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-24 07:02:15 +01:00
Files
2024-06-23 15:54:39 +08:00

147 lines
4.9 KiB
XML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 78d17c25eeecc78cb7098b1a01d42391e36af2c1 Maintainer: daijie Status: ready -->
<!-- CREDITS: mowangjuanzi, Luffy -->
<refentry xml:id="function.eval" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>eval</refname>
<refpurpose>把字符串作为PHP代码执行</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>mixed</type><methodname>eval</methodname>
<methodparam><type>string</type><parameter>code</parameter></methodparam>
</methodsynopsis>
<para>
把指定 <parameter>code</parameter> 作为 PHP 代码执行。
</para>
<para>
正在执行的代码继承调用 <function>eval</function> 所在行的<link
linkend="language.variables.scope">变量作用域</link>。该行中任何有效变量都可在执行的代码中读取和修改。但定义的所有函数和类都将在全局命名空间中定义。换句话说,编译器将执行的代码视为单独
<link linkend="function.include">included</link> 后的文件。
</para>
<caution>
<para>
<function>eval</function> 语言构造<emphasis>非常危险</emphasis>,因为它允许执行任意 PHP
代码。<emphasis>因此不鼓励使用它。</emphasis>如果已经仔细验证过除了使用此构造以外别无他法, 请多加注意不要在未事先正确验证的情况下<emphasis>将任何用户提供的数据传递到</emphasis>其中。
</para>
</caution>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>code</parameter></term>
<listitem>
<para>
需要被执行的字符串
</para>
<para>
代码不能包含打开/关闭
<link linkend="language.basic-syntax.phpmode">PHP tags</link>。比如,
<literal>'echo "Hi!";'</literal> 不能这样传入:
<literal>'&lt;?php echo "Hi!"; ?&gt;'</literal>。但仍然可以用合适的 PHP tag 来离开、重新进入 PHP 模式。比如
<literal>'echo "In PHP mode!"; ?&gt;In HTML mode!&lt;?php echo "Back in PHP mode!";'</literal>
</para>
<para>
除此之外,传入的必须是有效的 PHP 代码。所有的语句必须以分号结尾。比如
<literal>'echo "Hi!"'</literal> 会导致一个 parse error
<literal>'echo "Hi!";'</literal> 则会正常运行。
</para>
<para>
<literal>return</literal> 语句会立即中止当前字符串的执行。
</para>
<para>
代码执行的作用域是调用 <function>eval</function> 处的作用域。因此,<function>eval</function> 里任何的变量定义、修改,都会在函数结束后被保留。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
<function>eval</function> 返回 &null;,除非在执行的代码中 <literal>return</literal> 了一个值,函数返回传递给 <literal>return</literal> 的值。 PHP 7 开始,执行的代码里如果有一个 parse error<function>eval</function> 会抛出 <classname>ParseError</classname> 异常。在 PHP 7 之前,
如果在执行的代码中有 parse error<function>eval</function> 返回
&false;,之后的代码将正常执行。无法使用 <function>set_error_handler</function> 捕获 <function>eval</function> 中的解析错误。
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>eval</function> 例子 - 简单的文本合并</title>
<programlisting role="php">
<![CDATA[
<?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
This is a $string with my $name in it.
This is a cup with my coffee in it.
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
&note.language-construct;
&tip.ob-capture;
<note>
<para>
如果在执行的代码中产生了一个致命的错误fatal error整个脚本会退出。
</para>
</note>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>call_user_func</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->