首頁  >  文章  >  php教程  >  PHP中的可变参数函数和可选参数函数

PHP中的可变参数函数和可选参数函数

WBOY
WBOY原創
2016-06-14 00:02:001420瀏覽

1)可选参数函数.例如:

<div>
<span style="color: #000000;"></span><span style="color: #000000;">php<br></span><span style="color: #0000ff;">function</span><span style="color: #000000;"> add(</span><span style="color: #800080;">$var1</span><span style="color: #000000;">,</span><span style="color: #800080;">$var2</span><span style="color: #000000;">,</span><span style="color: #800080;">$var3</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">,</span><span style="color: #800080;">$var4</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">)<br>{<br>     </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$var1</span><span style="color: #000000;">+</span><span style="color: #800080;">$var2</span><span style="color: #000000;">+</span><span style="color: #800080;">$var3</span><span style="color: #000000;">+</span><span style="color: #800080;">$var4</span><span style="color: #000000;">;<br>}<br></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> add(</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">1</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;">输出2</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> add(</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">1</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">输出3</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> add(</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">1</span><span style="color: #000000;">);//输出4<br></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> add(</span><span style="color: #000000;">1</span><span style="color: #000000;">);      </span><span style="color: #008000;">//</span><span style="color: #008000;">出错:必须给出参数2</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> add(</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">1</span><span style="color: #000000;">,,</span><span style="color: #000000;">1</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">出错:不能漏掉一个可选参数而给出列表中最后一个可选参数</span><span style="color: #008000;"><br></span><span style="color: #000000;">?></span>
</div>

因为$var3和$var4在在定义函数是给出了默认值,如果不给他们传值,就是用默认值,所有是可选的.

2)可变参数函数

<div>
<span style="color: #000000;"></span><span style="color: #000000;">php<br>     </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> variable()<br>         {<br>                  </span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> </span><span style="color: #008080;">func_num_args</span><span style="color: #000000;">();         </span><span style="color: #008000;">//</span><span style="color: #008000;">输出参数个数</span><span style="color: #008000;"><br></span><span style="color: #000000;">                  </span><span style="color: #800080;">$varArray</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #008080;">func_get_args</span><span style="color: #000000;">;     </span><span style="color: #008000;">//</span><span style="color: #008000;">获取参数,返回参数数组</span><span style="color: #008000;"><br></span><span style="color: #000000;">                   </span><span style="color: #0000ff;">foreach</span><span style="color: #000000;">(</span><span style="color: #800080;">$varArray</span><span style="color: #000000;"> </span><span style="color: #0000ff;">as</span><span style="color: #000000;"> </span><span style="color: #800080;">$value</span><span style="color: #000000;">)<br>                     </span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> </span><span style="color: #800080;">$value</span><span style="color: #000000;">;<br>                  <br>                     </span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> </span><span style="color: #008080;">func_get_arg</span><span style="color: #000000;">;       </span><span style="color: #008000;">//</span><span style="color: #008000;">获取单个参数</span><span style="color: #008000;"><br></span><span style="color: #000000;">                   <br>         }<br></span><span style="color: #000000;">?></span>
</div>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn