Why is there a function create_function()? in PHP
function, php
Solution
Because of you can use it in many ways.. Even in an array!
$farr = array(
create_function('$x,$y', 'return "some trig: ".(sin($x) + $x*cos($y));'),
create_function('$x,$y', 'return "a hypotenuse: ".sqrt($x*$x + $y*$y);'),
create_function('$a,$b', $f1),
create_function('$a,$b', $f2),
create_function('$a,$b', $f3)
);
You're looking at just one example, but the use of this function is more complicated, you can use it in many different ways which will be easier then using the `function()`.
Like example#3 on PHP.net
<?php
$av = array("the ", "a ", "that ", "this ");
array_walk($av, create_function('&$v,$k', '$v = $v . "mango";'));
print_r($av);
?>
The above example will output:
Array
(
[0] => the mango
[1] => a mango
[2] => that mango
[3] => this mango
)
Problem
Why is there a function `create_function()` if I could just create the `function something() { ... }`. What is `create_function(string $args, string $code);` really meant for? For example should I want to `echo` a specific value, written long hand: ``` function sayHi($name){ echo 'Hi,' . $name; } //using it like: sayHi('Jacques Marais'); ``` But then using the `create_function()` method: ``` $sayHi = create_function('$name', 'echo \'Hi,\' . $name;'); //using it like: $sayHi('Jacques Marais'); ```