Maison > php教程 > php手册 > PHP中的可变参数函数和可选参数函数

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

WBOY
Libérer: 2016-06-14 00:02:00
original
1453 Les gens l'ont consulté

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>
Copier après la connexion

因为$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>
Copier après la connexion
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal