<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Evaluate a string as PHP code</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.die.html">die</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.exit.html">exit</a></div> <div class="up"><a href="ref.misc.html">Misc. Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.eval" class="refentry"> <div class="refnamediv"> <h1 class="refname">eval</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">eval</span> — <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="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>eval</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$code</code></span> )</div> <p class="para rdfs-comment"> Evaluates the given <em><code class="parameter">code</code></em> as PHP. </p> <div class="caution"><strong class="caution">Caution</strong> <p class="para"> The <span class="function"><strong>eval()</strong></span> language construct is <em class="emphasis">very dangerous</em> because it allows execution of arbitrary PHP code. <em class="emphasis">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 class="emphasis">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> <span class="term"><em><code class="parameter">code</code></em></span> <dd> <p class="para"> Valid PHP code to be evaluated. </p> <p class="para"> The code mustn't be wrapped in opening and closing <a href="language.basic-syntax.phpmode.html" class="link">PHP tags</a>, i.e. <em>'echo "Hi!";'</em> must be passed instead of <em>'<? echo "Hi!"; >'</em>. It is still possible to leave and reenter PHP mode though using the appropriate PHP tags, e.g. <em>'echo "In PHP mode!"; ?>In HTML mode!<? echo "Back in PHP mode!";'</em>. </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. <em>'echo "Hi!"'</em> for example will cause a parse error, whereas <em>'echo "Hi!";'</em> will work. </p> <p class="para"> A <em>return</em> 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> </dt> </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>NULL</code></strong> unless <em>return</em> is called in the evaluated code, in which case the value passed to <em>return</em> is returned. If there is a parse error in the evaluated code, <span class="function"><strong>eval()</strong></span> returns <strong><code>FALSE</code></strong> and execution of the following code continues 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.html" 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-3774"> <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"><?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">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 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.html" class="link">variable functions</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.html" 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.html" 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 class="member"> <span class="function"><a href="function.call-user-func.html" class="function" rel="rdfs-seeAlso">call_user_func()</a> - Call the callback given by the first parameter</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.die.html">die</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.exit.html">exit</a></div> <div class="up"><a href="ref.misc.html">Misc. Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>