函数的任意数目的参数

2016-3-31 liyangweb.com 李杨 PHP

你可能知道PHP允许你定义一个默认参数的函数。但你可能并不知道PHP还允许你定义一个完全任意的参数的函数

下面是一个示例向你展示了默认参数的函数:


// 两个默认参数的函数
  function foo($arg1 = '', $arg2 = '') {
  echo "arg1: $arg1\n";
  echo "arg2: $arg2\n";
  }
  foo('hello','world');
  
  foo();

现在我们来看一看一个不定参数的函数,其使用到了?func_get_args()方法:

  // 是的,形参列表为空
  function foo() {
  // 取得所有的传入参数的数组
  $args = func_get_args();
  foreach ($args as $k => $v) {
  echo "arg".($k+1).": $v\n";
  }
  }
  foo();
  
  foo('hello');
  
  foo('hello', 'world', 'again');

转自 http://blog.sina.com.cn/s/blog_5747a69601019lul.html

标签: PHP

评论:

nvusforex
2019-01-20 05:43
<a href="http://www.daddeaze.com/nike-huarache-air-force-1-lila-schwarz-chaussuresa">nike huarache air force 1 lila schwarz</a> <a href="http://www.debracousins.com/new-york-jets-12-joe-namath-white-throwback-jersey-nflc">new york jets 12 joe namath white throwback jersey</a> <a href="http://www.yankaripark.com/nike-air-max-turquoise-black-runningg">nike air max turquoise black</a> <a href="http://www.yicailottery.com/zdeno-chara-33-boston-bruins-reebok-premier-alternate-jersey-small-nflv">zdeno chara 33 boston bruins reebok premier alternate jersey small</a> <a href="http://www.calebcraw.com/nike-flyknit-racer-hyper-punch-black-electric-green-trainersa">nike flyknit racer hyper punch black electric green</a> <a href="http://www.liquidputter.com/nike-zoom-winflo-3-r%C3%B8d-gull-scarpec">nike zoom winflo 3 r酶d gull</a>
nvusforex http://www.nvusforex.com/
h8yuex60
2018-12-31 05:35
<a href="http://www.wwc3s90g.com/nike-air-max-lunar-90-arancia-verde-obuve">nike air max lunar 90 arancia verde</a> <a href="http://www.18ykuccs.com/air-max-95-cool-gris-dark-obsidian-schuhee">air max 95 cool gris dark obsidian</a> <a href="http://www.ofv56fjb.com/adidas-ultra-boost-uncaged-triple-svart-y3-kurpesc">adidas ultra boost uncaged triple svart y3</a> <a href="http://www.iwd661th.com/nike-free-run-4.0-dam%C3%A4nner-orange-lila-butyf">nike free run 4.0 dam盲nner orange lila</a> <a href="http://www.lv5sb333.com/kvinders-asics-gel-noosa-tri-9-billig-scarpee">kvinders asics gel noosa tri 9 billig</a> <a href="http://www.dzpzobo7.com/air-jordan-eclipse-bianca-verde-schuhea">air jordan eclipse bianca verde</a>
h8yuex60 http://www.h8yuex60.com/
blockchainmkt
2018-12-30 16:13
<a href="http://www.godsentmusic.com/nike-kobe-9-mid-rosa-oro-schuheg">nike kobe 9 mid rosa oro</a> <a href="http://www.itmontwitter.com/nike-free-run-3-5.0-femmes-or-noir-schuheb">nike free run 3 5.0 femmes or noir</a> <a href="http://www.kolsyreis.com/nike-free-3.0-ni%C3%B1os-chaussurese">nike free 3.0 ni帽os</a> <a href="http://www.ritaburton.com/bon-march%C3%A9-air-jordan-7-bordeaux-butyg">bon march茅 air jordan 7 bordeaux</a> <a href="http://www.blanchardsvac.com/air-jordan-8-sort-and-orange-scarpea">air jordan 8 sort and orange</a> <a href="http://www.cesafetyprod.com/nike-air-force-1-high-wei%C3%9F-braun-schuhec">nike air force 1 high wei脽 braun</a>
blockchainmkt http://www.blockchainmkt.com/
ictbel
2018-12-30 12:18
<a href="http://www.daddeaze.com/billig-air-jordan-3-menns-gull-runninge">billig air jordan 3 menns gull</a> <a href="http://www.debracousins.com/jordan-3-true-bl%C3%A5-grade-school-schuhea">jordan 3 true bl氓 grade school</a> <a href="http://www.yankaripark.com/air-jordan-ultra-fly-violet-or-scarpeb">air jordan ultra fly violet or</a> <a href="http://www.yicailottery.com/nike-air-max-2019-green-uk-shoeso">nike air max 2019 green uk</a> <a href="http://www.calebcraw.com/asics-tiger-gel-lyte-v-noir-kurpesb">asics tiger gel lyte v noir</a> <a href="http://www.liquidputter.com/nike-sb-v-rod-dark-loden-sport-red-zapatost">nike sb v rod dark loden sport red</a>
[url=http://www.ictbel.com/]ictbel[/url]
lepenequet
2018-12-29 22:48
<a href="http://www.rcnspecials.com/nike-huarache-team-r%C3%B8d-hyper-punch-obuvc">nike huarache team r酶d hyper punch</a> <a href="http://www.aprancheta.com/femminile-nike-free-run-5.0-rosso-bianca-scarpea">femminile nike free run 5.0 rosso bianca</a> <a href="http://www.getleanlearn.com/adidas-ultra-boost-triple-sort-k%C3%B8be-online-scarpef">adidas ultra boost triple sort k酶be online</a> <a href="http://www.hbshbuick.com/adidas-protator-absolion-powerswerve-obuvc">adidas protator absolion powerswerve</a> <a href="http://www.hvactampafl.com/salomon-speedcross-3-schwarz-schwarz-silber-metal-earrings-scarpea">salomon speedcross 3 schwarz schwarz silber metal earrings</a> <a href="http://www.zinmanlaw.com/air-max-zero-triple-noir-man-chaussuresg">air max zero triple noir man</a>
lepenequet http://www.lepenequet.com/

发表评论:

Powered by emlog 冀ICP备13011830号-1