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>