<code>PHP函数的定义方式: <span>1.</span>使用关键字“<span><span>function</span>”开始</span><span>2.</span>函数名可以是字母或下划线开头:<span><span>function</span><span>name</span><span>()</span></span><span>3.</span>在大括号中编写函数体: <span><span>function</span><span>name</span><span>()</span></span><span>{ //函数体 echo <span>'Eric'</span>; }</span> 调用方法为函数名+参数,例如:name();</code>
回傳值的函數
使用return關鍵字可以使函數傳回值,可以傳回包含陣列和物件的任意型別,如果省略了return,則預設傳回值為NULL。
<code><span><span><?php</span><span><span>function</span><span>sum</span><span>(<span>$a</span>, <span>$b</span>)</span> {</span><span>return</span><span>$a</span>+<span>$b</span>; } <span>//在这里调用函数取得返回值</span><span>/** * 初始化数组函数 */</span><span><span>function</span><span>initArray</span><span>()</span>{</span><span>$arr</span> = <span>array</span>(<span>'o'</span>=><span>'哦'</span>); <span>return</span><span>$arr</span>; } <span>$s</span> = sum(<span>1</span>,<span>2</span>); print_r(<span>$s</span>.<span>'<br>'</span>); <span>$arr</span> = initArray(); print_r(<span>$arr</span>[<span>'o'</span>]); <span>?></span></span></span></code>
以上就介紹了行動app介面程式設計技術-學習實作之PHP進階 函數,包含了方面的內容,希望對PHP教學有興趣的朋友有所幫助。