Heim  >  Artikel  >  php教程  >  PHP中的可变参数函数和可选参数函数

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

WBOY
WBOYOriginal
2016-06-14 00:02:001425Durchsuche

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>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Apache,PHP,MySQL的安装,配置Nächster Artikel:PHP中奇妙的String