Home  >  Article  >  Backend Development  >  What does a php callback function look like?

What does a php callback function look like?

怪我咯
怪我咯Original
2017-06-19 11:27:351929browse

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 &#39;msg1:&#39;.$msg1;
      echo &#39;<br/>&#39;;
      echo &#39;msg2:&#39;.$msg2;
   }
   $fnName = &#39;fnCallBack&#39;;//函数名
   $params = array(&#39;hello&#39;, &#39;world&#39;);//将要传入到函数里面的参数
   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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn