PHP function to run a function (based on string)?

Solution:1

Try call_user_func() http://php.net/manual/en/function.call-user-func.php

The first argument is a string representing the function name. The second argument can be a single argument, or an array of arguments.

Edit:

It will even support native functions like time(), as you asked. Try it out like so:

echo call_user_func( 'time' );

Solution:2

You can call a function using a variable containing the function name as well as pass any parameters to that function. Try:

<?php

$func = 'strtoupper';

$res = $func('i am uppercase');

echo $res; // I AM UPPERCASE

See variable functions and variable variables as well. As mentioned, call_user_func() works too.

For safety, you can call:

if (function_exists($func)) { ... }

before calling the function based to make sure you avoid errors. You can use ReflectionFunctionAbstract::getNumberOfParameters to determine the number of parameters the function accepts as well.