傳變數傳引用的區別
傳引用如:
<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教程有興趣的朋友有幫助。