PHP動態擷取函數參數步驟詳解

來源:互聯網
上載者:User
這次給大家帶來PHP動態擷取函數參數步驟詳解,PHP動態擷取函數參數的注意事項有哪些,下面就是實戰案例,一起來看一下。

PHP 在使用者自訂函數中支援可變數量的參數列表。其實很簡單,只需使用 func_num_args()func_get_arg() ,和 func_get_args() 函數即可。

可變參數並不需要特別的文法,參數列表仍按函數定義的方式傳遞給函數,並按通常的方式使用這些參數。

1. func_num_args — 返回傳入函數的參數總個數

int func_num_args ( void )

樣本

<?phpfunction demo (){  $numargs = func_num_args ();  echo "參數個數為: $numargs \n" ;}demo ( 'a' , 'b' , 'c' );

運行結果

參數個數為: 3

2. func_get_args — 返回傳入函數的參數列表

array func_get_args ( void )

樣本

<?phpfunction demo (){  $args = func_get_args();  echo "傳入的參數分別為:";  var_dump($args);}demo ( 'a' , 'b' , 'c' );

運行結果

傳入的參數分別為:
array (size=3)
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)

3. func_get_arg — 根據參數索引從參數列表返回參數值

mixed func_get_arg ( int $arg_num )

樣本

<?phpfunction demo (){  $numargs = func_num_args ();  echo "參數個數為: $numargs <br />" ;  $args = func_get_args();  if ( $numargs >= 2 ) {    echo "第二個參數為: " . func_get_arg ( 1 ) . "<br />" ;  }}demo ( 'a' , 'b' , 'c' );

運行結果

參數個數為: 3
第二個參數為: b

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

PHP實現紅包金額拆分演算法案例詳解

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.