What is call_user_func used for in PHP?

raina_vissora

New member
I've used it on the occasion when I needed to call one of several functions based on user input. It saves me the effort of doing a long, tedious if-statement. For example:

function example($param) {
$result = call_user_func($param);
}

instead of:

func other_example($param) {
if($param == 'some_value') {
$result = some_function();
}
elseif($param == 'some_other_value') {
$result = some_other_function();
}
...
}
 
We can directly call a function by its name, e.g.

my_function($arg1);

So why do we have an extra call_user_func function used to call other functions?

Thank you very much for your help.
 
I've used it on the occasion when I needed to call one of several functions based on user input. It saves me the effort of doing a long, tedious if-statement. For example:

function example($param) {
$result = call_user_func($param);
}

instead of:

func other_example($param) {
if($param == 'some_value') {
$result = some_function();
}
elseif($param == 'some_other_value') {
$result = some_other_function();
}
...
}
 
I've used it on the occasion when I needed to call one of several functions based on user input. It saves me the effort of doing a long, tedious if-statement. For example:

function example($param) {
$result = call_user_func($param);
}

instead of:

func other_example($param) {
if($param == 'some_value') {
$result = some_function();
}
elseif($param == 'some_other_value') {
$result = some_other_function();
}
...
}
 
I've used it on the occasion when I needed to call one of several functions based on user input. It saves me the effort of doing a long, tedious if-statement. For example:

function example($param) {
$result = call_user_func($param);
}

instead of:

func other_example($param) {
if($param == 'some_value') {
$result = some_function();
}
elseif($param == 'some_other_value') {
$result = some_other_function();
}
...
}
 
I've used it on the occasion when I needed to call one of several functions based on user input. It saves me the effort of doing a long, tedious if-statement. For example:

function example($param) {
$result = call_user_func($param);
}

instead of:

func other_example($param) {
if($param == 'some_value') {
$result = some_function();
}
elseif($param == 'some_other_value') {
$result = some_other_function();
}
...
}
 
I've used it on the occasion when I needed to call one of several functions based on user input. It saves me the effort of doing a long, tedious if-statement. For example:

function example($param) {
$result = call_user_func($param);
}

instead of:

func other_example($param) {
if($param == 'some_value') {
$result = some_function();
}
elseif($param == 'some_other_value') {
$result = some_other_function();
}
...
}
 
Back
Top