<HTML ><HEAD ><TITLE >eval</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" 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=ISO-8859-1"></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 Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.die.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.exit.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.eval" ></A >eval</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN55453" ></A ><P > (PHP 3, PHP 4 )</P >eval -- Evaluate a string as PHP code</DIV ><DIV CLASS="refsect1" ><A NAME="AEN55456" ></A ><H2 >Description</H2 >mixed <B CLASS="methodname" >eval</B > ( string code_str)<BR ></BR ><P > <B CLASS="function" >eval()</B > evaluates the string given in <TT CLASS="parameter" ><I >code_str</I ></TT > 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 <TT CLASS="parameter" ><I >code_str</I ></TT >. </P ><P > Also remember that variables given values under <B CLASS="function" >eval()</B > will retain these values in the main script afterwards. </P ><P > A <TT CLASS="literal" >return</TT > statement will terminate the evaluation of the string immediately. In PHP 4, <B CLASS="function" >eval()</B > returns <TT CLASS="constant" ><B >NULL</B ></TT > unless <A HREF="function.return.html" ><B CLASS="function" >return()</B ></A > is called in the evaluated code, in which case the value passed to <A HREF="function.return.html" ><B CLASS="function" >return()</B ></A > is returned. In PHP 3, <B CLASS="function" >eval()</B > does not return a value. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN55481" ></A ><P ><B >Example 1. <B CLASS="function" >eval()</B > example - simple text merge </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $string = 'cup'; $name = 'coffee'; $str = 'This is a $string with my $name in it.<br>'; echo $str; eval ("\$str = \"$str\";"); echo $str; ?></PRE ></TD ></TR ></TABLE ><P > The above example will show: <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 > </P ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="tip" ><BLOCKQUOTE CLASS="tip" ><P ><B >Tip: </B >As with anything that outputs its result directly to the browser, you can use the <A HREF="ref.outcontrol.html" >output-control functions</A > to capture the output of this function, and save it in a <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A > (for example).</P ></BLOCKQUOTE ></DIV ></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" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.exit.html" ACCESSKEY="N" >Next</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" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >exit</TD ></TR ></TABLE ></DIV ></BODY ></HTML >