Custom functions
Function Study ($username) {
Echo $username. ' Is study...<br/> ';
}
function Play ($username) {
echo $username. ' Is playing...<br/> ';
}
Custom Calls
function Dowhat ($funcName, $param) {
$funcName ($param);
}
Dowhat (' study ', ' King ');
Dowhat (' Play ', ' Lisi ');
The system call function//call_user_func-The first argument as a callback function
Mixed Call_user_func (callable $callback [, Mixed $parameter [, mixed $ ...])
Echo ' <br/> ';
Call_user_func (' study ', ' Liuzhao ');
Call_user_func (' Play ', ' Harry ');
Echo ' Liuzhao MD5 encryption: '. Call_user_func (' MD5 ', ' Liuzhao ');
Echo ' <br/> ';
Callback plus minus
function Add ($x, $y) {
return $x + $y;
}
function reduce ($x, $y) {
return $x-$y;
}
Function Calc ($funcName, $x, $y) {
Return $funcName ($x, $y);
}
Echo ' Echo Calc (' Add ', for each);
echo ' <br/> ';
Echo Calc (' Reduce ', for each);
Callback using Array_map to function the callback function on the cell of a given array
echo ' $arr =array (1,2,3,4,5);
function Test1 ($var) {
return $var * *;
}
$result =array_map (' test1 ', $arr);
Print_r ($arr);
echo ' <br/> ';
Print_r ($result);
Array_walk-uses a user-defined function to do callback processing for each element in the array
BOOL Array_walk (array & $array, callable $callback [, mixed $userdata = NULL])
$arr 1=array (1,2,3,4,5);
Reference, *=
Function Test2 (& $var) {
$var *=3;
}
Echo ' Array_walk ($arr 1, ' test2 ');
Print_r ($arr 1);
array_filter-filter cells in an array with callback functions
Array Array_filter (array $array [, callable $callback [, int $flag = 0]])
Filter Odd
echo ' $arr 2=array (1,2,3,4,5);
function Odd ($var) {
if ($var%2==0) {
return $var;
}
}
$res =array_filter ($arr 2, ' odd ');
Var_dump ($res);
PHP Callback Functions