传变量传引用的区别
传引用如:
<code><span>$num</span> = <span>0</span>; <span><span>function</span><span>test0</span><span>(<span>$num</span>)</span>{</span><span>//将变量的值副本传进去,而不是变量本身。</span><span>$num</span> = <span>123</span>; <span>echo</span><span>$num</span>; } test0(<span>$num</span>);<span>//输出:123</span><span>echo</span><span>$num</span>; <span>//输出:0</span><span>$num2</span> = <span>100</span>; <span><span>function</span><span>test1</span><span>(&<span>$val</span>)</span>{</span><span>//传变量地址,操作的是变量本身</span><span>$val</span> = <span>300</span>; <span>echo</span><span>$val</span>; } test1(<span>$num2</span>);<span>//输出:300</span><span>echo</span><span>$num2</span>; <span>//输出:300</span></code>
变量函数,即函数名是一个变量
<code><span>$temp</span> = <span>"king"</span>; <span>$name</span> = <span>"md5"</span>; <span>echo</span><span>$name</span>(<span>$temp</span>);<span>//调用md5方法,将king转成md5 </span></code>
回调函数,一个函数调用另一个函数,但是通过变量函数调用的方法实现,系统提供两个方法:call_user_func()、call_user_func_array(),两者的区别是前者参数是一个一个传,后者参数以数组的形式传。例子如下:
<code><span><span>function</span><span>sayName</span><span>(<span>$val</span>)</span>{</span><span>echo</span><span>"My name is {$val}"</span>; } <span><span>function</span><span>doWhat</span><span>(<span>$myFunction</span>,<span>$name</span>)</span>{</span><span>$myFunction</span>(<span>$name</span>); } <span>$name</span> = <span>"King"</span>; doWhat(<span>"sayName"</span>,<span>$name</span>);<span>//输出:My name is King.</span></code>
版权声明:本文为原创文章,转载需注明出处,文章观点只代表当时的看法,一定有不足,欢迎拍砖提醒,非常感谢!
以上就介绍了php学习笔记_函数类型,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。