|
PHP5中文手册
create_function(PHP 4 >= 4.0.1, PHP 5) create_function — Create an anonymous (lambda-style) function 说明
string create_function
( string $args
, string $code
)
Creates an anonymous function from the parameters passed, and returns a unique name for it. 参数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. \$avar.
返回值Returns a unique function name as a string, or FALSE on error. 范例
Example#1 Creating an anonymous function with create_function() You can use this function, to (for example) create a function from information gathered at run time:
<?php Or, perhaps to have general handler function that can apply a set of operations to a list of parameters:
Example#2 Making a general processing function with create_function()
<?php 上例将输出:
But perhaps the most common use for of lambda-style (anonymous) functions is to create callback functions, for example when using array_walk() or usort()
Example#3 Using anonymous functions as callback functions
<?php 上例将输出:
an array of strings ordered from shorter to longer
<?php 上例将输出:
sort it from longer to shorter
<?php 上例将输出:
|