<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >eval</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Miscellaneous Functions" HREF="ref.misc.html"><LINK REL="PREVIOUS" TITLE="die" HREF="function.die.html"><LINK REL="NEXT" TITLE="exit" HREF="function.exit.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.die.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.exit.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.eval" ></A >eval</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN125342" ></A ><P > (PHP 3, PHP 4, PHP 5)</P >eval -- Evaluate a string as PHP code</DIV ><DIV CLASS="refsect1" ><A NAME="AEN125345" ></A ><H2 >说明</H2 >mixed <B CLASS="methodname" >eval</B > ( string code_str )<BR ></BR ><P > Evaluates the string given in <CODE CLASS="parameter" >code_str</CODE > as PHP code. Among other things, this can be useful for storing code in a database text field for later execution. </P ><P > There are some factors to keep in mind when using <B CLASS="function" >eval()</B >. Remember that the string passed must be valid PHP code, including things like terminating statements with a semicolon so the parser doesn't die on the line after the <B CLASS="function" >eval()</B >, and properly escaping things in <CODE CLASS="parameter" >code_str</CODE >. To mix HTML output and PHP code you can use a closing PHP tag to leave PHP mode. </P ><P > Also remember that variables given values under <B CLASS="function" >eval()</B > will retain these values in the main script afterwards. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN125361" ></A ><H2 >参数</H2 ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >code_str</CODE ></DT ><DD ><P > The code string to be evaluated. <CODE CLASS="parameter" >code_str</CODE > does not have to contain <A HREF="language.basic-syntax.html#language.basic-syntax.phpmode" >PHP Opening tags</A >. </P ><P > A <TT CLASS="literal" >return</TT > statement will immediately terminate the evaluation of the string . </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN125374" ></A ><H2 >返回值</H2 ><P > As of PHP 4, <B CLASS="function" >eval()</B > returns <TT CLASS="constant" ><B >NULL</B ></TT > unless <TT CLASS="literal" >return</TT > is called in the evaluated code, in which case the value passed to <TT CLASS="literal" >return</TT > is returned. In case of a parse error in the evaluated code, <B CLASS="function" >eval()</B > returns <TT CLASS="constant" ><B >FALSE</B ></TT >. </P ><P > In PHP 3, <B CLASS="function" >eval()</B > does not return a value. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN125385" ></A ><H2 >范例</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN125388" ></A ><P ><B >例 1. <B CLASS="function" >eval()</B > example - simple text merge</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$string </font><font color="#007700">= </font><font color="#DD0000">'cup'</font><font color="#007700">;<br /></font><font color="#0000BB">$name </font><font color="#007700">= </font><font color="#DD0000">'coffee'</font><font color="#007700">;<br /></font><font color="#0000BB">$str </font><font color="#007700">= </font><font color="#DD0000">'This is a $string with my $name in it.'</font><font color="#007700">;<br />echo </font><font color="#0000BB">$str</font><font color="#007700">. </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />eval(</font><font color="#DD0000">"</font><font color="#007700">\$</font><font color="#DD0000">str = </font><font color="#007700">\"</font><font color="#DD0000">$str</font><font color="#007700">\"</font><font color="#DD0000">;"</font><font color="#007700">);<br />echo </font><font color="#0000BB">$str</font><font color="#007700">. </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >上例将输出:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >This is a $string with my $name in it. This is a cup with my coffee in it.</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN125394" ></A ><H2 >注释</H2 ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B >由于这是一个语言结构而非函数,因此它无法被<A HREF="functions.variable-functions.html" >变量函数</A >调用。</P ></BLOCKQUOTE ></DIV ><DIV CLASS="tip" ><BLOCKQUOTE CLASS="tip" ><P ><B >提示: </B >为了防止程序直接将结果输出到浏览器,可以使用<A HREF="ref.outcontrol.html" >输出控制函数</A >来捕获此函数的输出,并把它们保存到一个例如 <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A > 类型的变量中。</P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > In case of a fatal error in the evaluated code, the whole script exits. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN125405" ></A ><H2 >参见</H2 ><P > <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><A HREF="function.call-user-func.html" ><B CLASS="function" >call_user_func()</B ></A ></TD ></TR ></TBODY ></TABLE ><P ></P > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.die.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.exit.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >die</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.misc.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >exit</TD ></TR ></TABLE ></DIV ></BODY ></HTML >