<!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>Create an anonymous (lambda-style) function</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.call-user-func.html">call_user_func</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.forward-static-call-array.html">forward_static_call_array</a></div> <div class="up"><a href="ref.funchand.html">Function handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.create-function" class="refentry"> <div class="refnamediv"> <h1 class="refname">create_function</h1> <p class="verinfo">(PHP 4 >= 4.0.1, PHP 5)</p><p class="refpurpose"><span class="refname">create_function</span> — <span class="dc-title">Create an anonymous (lambda-style) function</span></p> </div> <div class="refsect1 description" id="refsect1-function.create-function-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>create_function</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$args</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$code</code></span> )</div> <p class="para rdfs-comment"> Creates an anonymous function from the parameters passed, and returns a unique name for it. </p> <div class="caution"><strong class="caution">Caution</strong> <p class="para"> This function internally performs an <span class="function"><a href="function.eval.html" class="function">eval()</a></span> and as such has the same security issues as <span class="function"><a href="function.eval.html" class="function">eval()</a></span>. Additionally it has bad performance and memory usage characteristics. </p> <p class="para"> If you are using PHP 5.3.0 or newer a native <a href="functions.anonymous.html" class="link">anonymous function</a> should be used instead. </p> </div> </div> <div class="refsect1 parameters" id="refsect1-function.create-function-parameters"> <h3 class="title">Parameters</h3> <p class="para"> Usually these parameters will be passed as single quote delimited strings. The reason for using single quoted strings, is to protect the variable names from parsing, otherwise, if you use double quotes there will be a need to escape the variable names, e.g. <em>\$avar</em>. <dl> <dt> <span class="term"><em><code class="parameter">args</code></em></span> <dd> <p class="para"> The function arguments. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">code</code></em></span> <dd> <p class="para"> The function code. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.create-function-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns a unique function name as a string, or <strong><code>FALSE</code></strong> on error. </p> </div> <div class="refsect1 examples" id="refsect1-function.create-function-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-5103"> <p><strong>Example #1 Creating an anonymous function with <span class="function"><strong>create_function()</strong></span> </strong></p> <div class="example-contents"><p> You can use this function, to (for example) create a function from information gathered at run time: </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$newfunc </span><span style="color: #007700">= </span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$a,$b'</span><span style="color: #007700">, </span><span style="color: #DD0000">'return "ln($a) + ln($b) = " . log($a * $b);'</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"New anonymous function: </span><span style="color: #0000BB">$newfunc</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$newfunc</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">M_E</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// outputs<br />// New anonymous function: lambda_1<br />// ln(2) + ln(2.718281828459) = 1.6931471805599<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> Or, perhaps to have general handler function that can apply a set of operations to a list of parameters: </p> <p class="para"> <div class="example" id="example-5104"> <p><strong>Example #2 Making a general processing function with <span class="function"><strong>create_function()</strong></span> </strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">process</span><span style="color: #007700">(</span><span style="color: #0000BB">$var1</span><span style="color: #007700">, </span><span style="color: #0000BB">$var2</span><span style="color: #007700">, </span><span style="color: #0000BB">$farr</span><span style="color: #007700">)<br />{<br /> foreach (</span><span style="color: #0000BB">$farr </span><span style="color: #007700">as </span><span style="color: #0000BB">$f</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$f</span><span style="color: #007700">(</span><span style="color: #0000BB">$var1</span><span style="color: #007700">, </span><span style="color: #0000BB">$var2</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #FF8000">// create a bunch of math functions<br /></span><span style="color: #0000BB">$f1 </span><span style="color: #007700">= </span><span style="color: #DD0000">'if ($a >=0) {return "b*a^2 = ".$b*sqrt($a);} else {return false;}'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$f2 </span><span style="color: #007700">= </span><span style="color: #DD0000">"return \"min(b^2+a, a^2,b) = \".min(\$a*\$a+\$b,\$b*\$b+\$a);"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$f3 </span><span style="color: #007700">= </span><span style="color: #DD0000">'if ($a > 0 && $b != 0) {return "ln(a)/b = ".log($a)/$b; } else { return false; }'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$farr </span><span style="color: #007700">= array(<br /> </span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$x,$y'</span><span style="color: #007700">, </span><span style="color: #DD0000">'return "some trig: ".(sin($x) + $x*cos($y));'</span><span style="color: #007700">),<br /> </span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$x,$y'</span><span style="color: #007700">, </span><span style="color: #DD0000">'return "a hypotenuse: ".sqrt($x*$x + $y*$y);'</span><span style="color: #007700">),<br /> </span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$a,$b'</span><span style="color: #007700">, </span><span style="color: #0000BB">$f1</span><span style="color: #007700">),<br /> </span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$a,$b'</span><span style="color: #007700">, </span><span style="color: #0000BB">$f2</span><span style="color: #007700">),<br /> </span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$a,$b'</span><span style="color: #007700">, </span><span style="color: #0000BB">$f3</span><span style="color: #007700">)<br /> );<br /><br />echo </span><span style="color: #DD0000">"\nUsing the first array of anonymous functions\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"parameters: 2.3445, M_PI\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">process</span><span style="color: #007700">(</span><span style="color: #0000BB">2.3445</span><span style="color: #007700">, </span><span style="color: #0000BB">M_PI</span><span style="color: #007700">, </span><span style="color: #0000BB">$farr</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// now make a bunch of string processing functions<br /></span><span style="color: #0000BB">$garr </span><span style="color: #007700">= array(<br /> </span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$b,$a'</span><span style="color: #007700">, </span><span style="color: #DD0000">'if (strncmp($a, $b, 3) == 0) return "** \"$a\" '</span><span style="color: #007700">.<br /> </span><span style="color: #DD0000">'and \"$b\"\n** Look the same to me! (looking at the first 3 chars)";'</span><span style="color: #007700">),<br /> </span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$a,$b'</span><span style="color: #007700">, </span><span style="color: #DD0000">'; return "CRCs: " . crc32($a) . ", ".crc32($b);'</span><span style="color: #007700">),<br /> </span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$a,$b'</span><span style="color: #007700">, </span><span style="color: #DD0000">'; return "similar(a,b) = " . similar_text($a, $b, &$p) . "($p%)";'</span><span style="color: #007700">)<br /> );<br />echo </span><span style="color: #DD0000">"\nUsing the second array of anonymous functions\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">process</span><span style="color: #007700">(</span><span style="color: #DD0000">"Twas brilling and the slithy toves"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Twas the night"</span><span style="color: #007700">, </span><span style="color: #0000BB">$garr</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> Using the first array of anonymous functions parameters: 2.3445, M_PI some trig: -1.6291725057799 a hypotenuse: 3.9199852871011 b*a^2 = 4.8103313314525 min(b^2+a, a^2,b) = 8.6382729035898 ln(a)/b = 0.27122299212594 Using the second array of anonymous functions ** "Twas the night" and "Twas brilling and the slithy toves" ** Look the same to me! (looking at the first 3 chars) CRCs: -725381282, 342550513 similar(a,b) = 11(45.833333333333%) </pre></div> </div> </div> </p> <p class="para"> But perhaps the most common use for of lambda-style (anonymous) functions is to create callback functions, for example when using <span class="function"><a href="function.array-walk.html" class="function">array_walk()</a></span> or <span class="function"><a href="function.usort.html" class="function">usort()</a></span> </p> <p class="para"> <div class="example" id="example-5105"> <p><strong>Example #3 Using anonymous functions as callback functions</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$av </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"the "</span><span style="color: #007700">, </span><span style="color: #DD0000">"a "</span><span style="color: #007700">, </span><span style="color: #DD0000">"that "</span><span style="color: #007700">, </span><span style="color: #DD0000">"this "</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_walk</span><span style="color: #007700">(</span><span style="color: #0000BB">$av</span><span style="color: #007700">, </span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'&$v,$k'</span><span style="color: #007700">, </span><span style="color: #DD0000">'$v = $v . "mango";'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$av</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> Array ( [0] => the mango [1] => a mango [2] => that mango [3] => this mango ) </pre></div> </div> <div class="example-contents"><p> an array of strings ordered from shorter to longer </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$sv </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"small"</span><span style="color: #007700">, </span><span style="color: #DD0000">"larger"</span><span style="color: #007700">, </span><span style="color: #DD0000">"a big string"</span><span style="color: #007700">, </span><span style="color: #DD0000">"it is a string thing"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$sv</span><span style="color: #007700">);<br /><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> Array ( [0] => small [1] => larger [2] => a big string [3] => it is a string thing ) </pre></div> </div> <div class="example-contents"><p> sort it from longer to shorter </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />usort</span><span style="color: #007700">(</span><span style="color: #0000BB">$sv</span><span style="color: #007700">, </span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$a,$b'</span><span style="color: #007700">,</span><span style="color: #DD0000">'return strlen($b) - strlen($a);'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$sv</span><span style="color: #007700">);<br /><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> Array ( [0] => it is a string thing [1] => a big string [2] => larger [3] => small ) </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.create-function-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="functions.anonymous.html" class="link">Anonymous functions</a></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.call-user-func.html">call_user_func</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.forward-static-call-array.html">forward_static_call_array</a></div> <div class="up"><a href="ref.funchand.html">Function handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>