Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-14 00:02:00
Original
1419 people have browsed it

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>
Copy after login

因为$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>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!