PHP函數功能講解_PHP教程

來源:互聯網
上載者:User
PHP的真正威力源自於它的函數,但有些PHP函數並沒有得到充分的利用,也並不是所有人都會從頭到尾一頁一頁地閱讀手冊和函數參考,這裡將向您介紹這些實用的函數和功能。

  1、任意參數數目的函數
  你可能已經知道,PHP允許定義選擇性參數的函數。但也有完全允許任意數目的函數參數的方法。以下是選擇性參數的例子:

以下為引用的內容:
//functionwith2optionalarguments
functionfoo($arg1=”,$arg2=”){
echo“arg1:$arg1”;
echo“arg2:$arg2”;
}

foo(‘hello’,world’);
/*prints:
arg1:hello
arg2:world
*/

foo();
/*prints:
arg1:
20. arg2:
*/

  現在讓我們看看如何建立能夠接受任何參數數目的函數。這一次需要使用func_get_args()函數:

以下為引用的內容:
//yes,theargumentlistcanbeempty
functionfoo(){
//returnsanarrayofallpassedarguments
$args=func_get_args();
foreach($argsas$k=>$v){
echo“arg”.($k 1).”:$v”;
}
}

foo();
/*printsnothing*/

foo(‘hello’);
/*prints
arg1:hello
*/

foo(‘hello’,‘world’,‘again’);
/*prints
arg1:hello
arg2:world
arg3:again
*/

http://www.bkjia.com/PHPjc/486209.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/486209.htmlTechArticlePHP的真正威力源自於它的函數,但有些PHP函數並沒有得到充分的利用,也並不是所有人都會從頭到尾一頁一頁地閱讀手冊和函數參考,這裡...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.