PHP create_function() Function
The PHP create_function() function is used to create an anonymous (lambda-style) function from the parameters passed, and returns a unique name for it.
Usually these parameters is passed as single quote delimited strings. The reason for using single quoted strings, is to protect the variable names from parsing, otherwise, in case of double quotes there will be a need to escape the variable names, e.g. \$avar.
Syntax
create_function(args, code)
Parameters
args |
Required. Specify the function arguments. |
code |
Optional. Specify the function code. |
Return Value
Returns a unique function name as a string, or false on error.
Example: creating an anonymous function using create_function()
The example below shows how to create an anonymous function using create_function() function.
<?php $newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);'); echo "New anonymous function: $newfunc\n"; echo $newfunc(2, M_E) . "\n"; ?>
The output of the above code will be:
New anonymous function: lambda_1 ln(2) + ln(2.718281828459) = 1.6931471805599
Example: making a general processing function with create_function()
Consider the example below where this function is used to make a general processing function.
<?php function process($val1, $val2, $arr) { foreach ($arr as $val) { echo $val($val1, $val2)."\n"; } } //creating a bunch of math functions $f1 = 'if ($a >=0) {return "b*a^2 = ".$b*sqrt($a);} else {return false;}'; $f2 = "return \"min(b^2+a, a^2, b) = \".min(\$a*\$a+\$b,\$b*\$b+\$a);"; $farr = array( create_function('$x,$y', 'return "a hypotenuse: ".sqrt($x*$x + $y*$y);'), create_function('$a,$b', $f1), create_function('$a,$b', $f2) ); echo "\nUsing the first array of anonymous functions\n"; echo "parameters: 2, 4 \n"; process(2, 4, $farr); //now making a bunch of string processing functions $sarr = array( create_function('$a, $b', 'return "Lower case : " . strtolower($a.$b) ;'), create_function('$a, $b', 'return "Similar Character : " . similar_text($a, $b, $percent);') ); echo "\nUsing the second array of anonymous functions\n"; process("Hello", " World!", $sarr); ?>
The output of the above code will be:
Using the first array of anonymous functions parameters: 2, 4 a hypotenuse: 4.4721359549996 b*a^2 = 5.6568542494924 min(b^2+a, a^2, b) = 8 Using the second array of anonymous functions Lower case : hello world! Similar Character : 1
Example: using anonymous functions as callback functions
The example below describes how the anonymous function created by using this function is used as callback function.
<?php $arr = array("small", "good", "big", "tasty"); array_walk($arr, create_function('&$v,$k', '$v = $v." orange";')); print_r($arr); array_walk($arr, create_function('&$v,$k', '$v = ucwords($v);')); print_r($arr); ?>
The output of the above code will be:
Array ( [0] => small orange [1] => good orange [2] => big orange [3] => tasty orange ) Array ( [0] => Small Orange [1] => Good Orange [2] => Big Orange [3] => Tasty Orange )
❮ PHP Function Handling Reference