Home > Article > Backend Development > PHP study notes_function type
The difference between passing variables and passing references
Passing a reference such as:
<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>
Variable function, that is, the function name is a variable
<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>
Callback function, one function calls another function, but it is implemented through variable function calling. The system provides two methods: call_user_func(), call_user_func_array( ), the difference between the two is that the parameters of the former are passed one by one, while the parameters of the latter are passed in the form of an array. Examples are as follows:
<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>
Copyright statement: This article is an original article. Reprints must indicate the source. The views in the article only represent the views at the time. There must be shortcomings. Welcome to remind me. Thank you very much!
The above introduces the PHP learning notes_function types, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.