-2
$scripts[0] = 'Say Hello';
$scripts[1] = 'Dont Say Hello';
$scripts[2] = 'Welcome';

$function_name = str_replace(' ', '_', $scripts[0]);   //Name of the function "Say_Hello"

echo $function_name('Jolly');   // Should echo "Hello Jolly"

I have a huge array with full of names which are associated with functions. In normal ways, if we are to call the function Say_Hello(), we do like this : Say_Hello('Jolly'). But in my case above, the name of the function is stored in a variable so how can I call it?

Thanks

4

1 に答える 1

1

PHP関数をチェックしてくださいcall_user_func

call_user_func —最初のパラメーターで指定されたコールバックを呼び出します

例:

<?php
error_reporting(E_ALL);
function increment(&$var)
{
    $var++;
}

$a = 0;
call_user_func('increment', $a);
echo $a."\n";

call_user_func_array('increment', array(&$a)); // You can use this instead before PHP 5.3
echo $a."\n";
?>
于 2012-08-29T16:14:29.667 に答える