课程笔记
  • 所属章节课程:php自定义函数之回调函数

    <?php function Myself($one,$two,$func){ // examine that if a function,if not,cease execute these code,return false. if(!is_callable($func)){ return false; } echo $one+$two+$func($one,$two); } //Sum the $one and $two,and then pass them into the function($func) execute it. //$func is a Variable function // let us define several function and try function plusx2($foo,$bar){ $result=($foo+$bar)*2; return $result; function minus($one,$two){ $result=($foo-$bar); return $result; } //call function,Myself,Pass the parament into the function, and try it. echo Myself(20,10,'plusx2'); echo Myself(20,10,'minus'); ?> 处理过程: 1.将para1,para2赋值给$one,$two,再将变量函数Varibale function assigned to $function 2.在回调函数中判断第三个元素是否为函数,不是则return false 并停止执行 3.如果是,则执行代码段,并将para1,para2带入变量函数中 4.$func是可变的,可以是不同的函数,将para1,para2赋值给变量函数的para1,para2 5.执行后将结果返回至回调函数中 6.得到运算结果 回调函数即子啊一个函数里面,在传入函数名,将函数名加上括号,识别为变量函数配合执行

    2018-08-190个赞