Home > Article > Backend Development > What does a php callback function look like?
hpMany of the built-in functions use callback functions, such as:
array_filter — Use callback functions to filter units in the array.
array_diff_ukey — Use the callback function to compare the key names to calculate the difference set of the array.
The callback function here seems to be no different from the ordinary function. Please tell me, What does the php callback function look like? On what principle does it operate?
Callback functions are those functions that are written by oneself, but are not adjusted by oneself, but by others.
Just like the odd() and even() functions below.
<?php function odd($var) { return($var % 2 == 1); } function even($var) { return($var % 2 == 0); } $array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5); $array2 = array(6, 7, 8, 9, 10, 11, 12); echo "Odd :\n"; print_r(array_filter($array1, "odd"));//这里把array1的值依次传入到odd这个函数里面,这种方式就称为回调 echo "Even:\n"; print_r(array_filter($array2, "even")); ?>
The following example implements function callback
<? function fnCallBack($msg1, $msg2) { echo 'msg1:'.$msg1; echo '<br/>'; echo 'msg2:'.$msg2; } $fnName = 'fnCallBack';//函数名 $params = array('hello', 'world');//将要传入到函数里面的参数 call_user_func_array($fnName, $params); ?>
The above is the detailed content of What does a php callback function look like?. For more information, please follow other related articles on the PHP Chinese website!