i am trying to use this code:
$arr = [
function ($a) {
return $a + $a;
},
function ($b) {
return $b * $b;
},
function ($c,$cc) {
return $c % $cc - $c;
},
function ($d) {
return $d + 4 / $d;
}
];
how can i pass parameters to this functions? i have already learned this statment for function with no arguments. for example: function () { echo 'somethings'; } that's mean this function in an array don't have incoming arguments.
$test = rand (0,1);
echo $myarr[$test]();
In addition
can i change (rand) statment with other statment in the above code snippet?
You can do the following:
Your array:
$arr = [
function ($a) {
return $a + $a;
},
function ($b) {
return $b * $b;
},
function ($c,$cc) {
return $c % $cc - $c;
},
function ($d) {
return $d + 4 / $d;
}
];
Your rand
$test = rand(0,1);
---- note ----
you can change your rand() to any other statement provided that it an existing index of the $arr array. in order to know if $test value is an index which exists in the $arr array, you can do
if (isset($arr[$test])) {}
---- end note ---
to call the functions
$returnedValue = call_user_func($arr[$test], 10);
echo $returnedValue;