PHP – Calling function in Variable
A straightforward example:
function hello($name) { echo 'hello ' . $name; } $var = 'hello'; $var('Abdullah'); // out put 'hello Abdullah'
Next, Lambda-style funcitons or anonymous functions is a functions that created dynamically and store in variable.
Use create_function()
to create the functions and there’s two parameters required which the first one is the function arguments and the second one is the function’s body.
$func = null; $refactor = create_function('$a','return $a * ' . $_REQUEST['factor'] . ';'); if(count($_POST) > 0) { $func = create_function('$a','return trim($_POST[$a]);'); } else if(count($_GET) > 0) { $func = create_function('$a','return strtoupper($_GET[$a]);'); } else { $func = create_function('$a','return 0;'); } echo '<p>' . $func('file') . '</p>'; echo '<p>' . $refactor(5) . '</p>';