PHP Callback Functions

Source: Internet
Author: User
Tags md5 encryption

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.