<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >create_function</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Function Handling Functions" HREF="ref.funchand.html"><LINK REL="PREVIOUS" TITLE="call_user_func" HREF="function.call-user-func.html"><LINK REL="NEXT" TITLE="func_get_arg" HREF="function.func-get-arg.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.call-user-func.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.func-get-arg.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.create-function" ></A >create_function</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN62701" ></A ><P > (PHP 4 >= 4.0.1, PHP 5)</P >create_function -- Create an anonymous (lambda-style) function</DIV ><DIV CLASS="refsect1" ><A NAME="AEN62704" ></A ><H2 >说明</H2 >string <B CLASS="methodname" >create_function</B > ( string args, string code )<BR ></BR ><P > Creates an anonymous function from the parameters passed, and returns a unique name for it. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN62716" ></A ><H2 >参数</H2 ><P > 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. <TT CLASS="literal" >\$avar</TT >. <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >args</CODE ></DT ><DD ><P > The function arguments. </P ></DD ><DT ><CODE CLASS="parameter" >code</CODE ></DT ><DD ><P > The function code. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN62731" ></A ><H2 >返回值</H2 ><P > Returns a unique function name as a string, or <TT CLASS="constant" ><B >FALSE</B ></TT > on error. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN62735" ></A ><H2 >范例</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN62738" ></A ><P ><B >例 1. Creating an anonymous function with <B CLASS="function" >create_function()</B > </B ></P ><P > You can use this function, to (for example) create a function from information gathered at run time: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$newfunc </font><font color="#007700">= </font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$a,$b'</font><font color="#007700">, </font><font color="#DD0000">'return "ln($a) + ln($b) = " . log($a * $b);'</font><font color="#007700">);<br />echo </font><font color="#DD0000">"New anonymous function: $newfunc</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />echo </font><font color="#0000BB">$newfunc</font><font color="#007700">(</font><font color="#0000BB">2</font><font color="#007700">, </font><font color="#0000BB">M_E</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /></font><font color="#FF8000">// outputs<br />// New anonymous function: lambda_1<br />// ln(2) + ln(2.718281828459) = 1.6931471805599<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > Or, perhaps to have general handler function that can apply a set of operations to a list of parameters: </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN62745" ></A ><P ><B >例 2. Making a general processing function with <B CLASS="function" >create_function()</B > </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">function </font><font color="#0000BB">process</font><font color="#007700">(</font><font color="#0000BB">$var1</font><font color="#007700">, </font><font color="#0000BB">$var2</font><font color="#007700">, </font><font color="#0000BB">$farr</font><font color="#007700">)<br />{<br /> foreach (</font><font color="#0000BB">$farr </font><font color="#007700">as </font><font color="#0000BB">$f</font><font color="#007700">) {<br /> echo </font><font color="#0000BB">$f</font><font color="#007700">(</font><font color="#0000BB">$var1</font><font color="#007700">, </font><font color="#0000BB">$var2</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /> }<br />}<br /><br /></font><font color="#FF8000">// create a bunch of math functions<br /></font><font color="#0000BB">$f1 </font><font color="#007700">= </font><font color="#DD0000">'if ($a >=0) {return "b*a^2 = ".$b*sqrt($a);} else {return false;}'</font><font color="#007700">;<br /></font><font color="#0000BB">$f2 </font><font color="#007700">= </font><font color="#DD0000">"return \"min(b^2+a, a^2,b) = \".min(\$a*\$a+\$b,\$b*\$b+\$a);"</font><font color="#007700">;<br /></font><font color="#0000BB">$f3 </font><font color="#007700">= </font><font color="#DD0000">'if ($a > 0 && $b != 0) {return "ln(a)/b = ".log($a)/$b; } else { return false; }'</font><font color="#007700">;<br /></font><font color="#0000BB">$farr </font><font color="#007700">= array(<br /> </font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$x,$y'</font><font color="#007700">, </font><font color="#DD0000">'return "some trig: ".(sin($x) + $x*cos($y));'</font><font color="#007700">),<br /> </font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$x,$y'</font><font color="#007700">, </font><font color="#DD0000">'return "a hypotenuse: ".sqrt($x*$x + $y*$y);'</font><font color="#007700">),<br /> </font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$a,$b'</font><font color="#007700">, </font><font color="#0000BB">$f1</font><font color="#007700">),<br /> </font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$a,$b'</font><font color="#007700">, </font><font color="#0000BB">$f2</font><font color="#007700">),<br /> </font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$a,$b'</font><font color="#007700">, </font><font color="#0000BB">$f3</font><font color="#007700">)<br /> );<br /><br />echo </font><font color="#DD0000">"\nUsing the first array of anonymous functions\n"</font><font color="#007700">;<br />echo </font><font color="#DD0000">"parameters: 2.3445, M_PI\n"</font><font color="#007700">;<br /></font><font color="#0000BB">process</font><font color="#007700">(</font><font color="#0000BB">2.3445</font><font color="#007700">, </font><font color="#0000BB">M_PI</font><font color="#007700">, </font><font color="#0000BB">$farr</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// now make a bunch of string processing functions<br /></font><font color="#0000BB">$garr </font><font color="#007700">= array(<br /> </font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$b,$a'</font><font color="#007700">, </font><font color="#DD0000">'if (strncmp($a, $b, 3) == 0) return "** \"$a\" '</font><font color="#007700">.<br /> </font><font color="#DD0000">'and \"$b\"\n** Look the same to me! (looking at the first 3 chars)";'</font><font color="#007700">),<br /> </font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$a,$b'</font><font color="#007700">, </font><font color="#DD0000">'; return "CRCs: " . crc32($a) . " , ".crc32(b);'</font><font color="#007700">),<br /> </font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$a,$b'</font><font color="#007700">, </font><font color="#DD0000">'; return "similar(a,b) = " . similar_text($a, $b, &$p) . "($p%)";'</font><font color="#007700">)<br /> );<br />echo </font><font color="#DD0000">"\nUsing the second array of anonymous functions\n"</font><font color="#007700">;<br /></font><font color="#0000BB">process</font><font color="#007700">(</font><font color="#DD0000">"Twas brilling and the slithy toves"</font><font color="#007700">, </font><font color="#DD0000">"Twas the night"</font><font color="#007700">, </font><font color="#0000BB">$garr</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" >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 , 1908338681 similar(a,b) = 11(45.833333333333%)</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > But perhaps the most common use for of lambda-style (anonymous) functions is to create callback functions, for example when using <A HREF="function.array-walk.html" ><B CLASS="function" >array_walk()</B ></A > or <A HREF="function.usort.html" ><B CLASS="function" >usort()</B ></A > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN62755" ></A ><P ><B >例 3. Using anonymous functions as callback functions</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$av </font><font color="#007700">= array(</font><font color="#DD0000">"the "</font><font color="#007700">, </font><font color="#DD0000">"a "</font><font color="#007700">, </font><font color="#DD0000">"that "</font><font color="#007700">, </font><font color="#DD0000">"this "</font><font color="#007700">);<br /></font><font color="#0000BB">array_walk</font><font color="#007700">(</font><font color="#0000BB">$av</font><font color="#007700">, </font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'&$v,$k'</font><font color="#007700">, </font><font color="#DD0000">'$v = $v . "mango";'</font><font color="#007700">));<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$av</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" >Array ( [0] => the mango [1] => a mango [2] => that mango [3] => this mango )</PRE ></TD ></TR ></TABLE ><P > an array of strings ordered from shorter to longer </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /><br />$sv </font><font color="#007700">= array(</font><font color="#DD0000">"small"</font><font color="#007700">, </font><font color="#DD0000">"larger"</font><font color="#007700">, </font><font color="#DD0000">"a big string"</font><font color="#007700">, </font><font color="#DD0000">"it is a string thing"</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$sv</font><font color="#007700">);<br /><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" >Array ( [0] => small [1] => larger [2] => a big string [3] => it is a string thing )</PRE ></TD ></TR ></TABLE ><P > sort it from longer to shorter </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /><br />usort</font><font color="#007700">(</font><font color="#0000BB">$sv</font><font color="#007700">, </font><font color="#0000BB">create_function</font><font color="#007700">(</font><font color="#DD0000">'$a,$b'</font><font color="#007700">,</font><font color="#DD0000">'return strlen($b) - strlen($a);'</font><font color="#007700">));<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$sv</font><font color="#007700">);<br /><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" >Array ( [0] => it is a string thing [1] => a big string [2] => larger [3] => small )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </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.call-user-func.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.func-get-arg.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >call_user_func</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.funchand.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >func_get_arg</TD ></TR ></TABLE ></DIV ></BODY ></HTML >