Explanation of variable parameter functions and optional parameter functions in php

怪我咯
Release: 2023-03-11 16:48:01
Original
2980 people have browsed it

1) Optional parameters Function . For example:

<?php
function add($var1,$var2,$var3=0,$var4=0)
{
     return $var1+$var2+$var3+$var4;
}
echo add(1,1);    //输出2
echo add(1,1,1); //输出3
echo add(1,1,1,1);//输出4
echo add(1);      //出错:必须给出参数2
echo add(1,1,,1);//出错:不能漏掉一个可选参数而给出列表中最后一个可选参数
?>
Copy after login

Because $var3 and $var4 are given default values ​​in defining the function , if not Pass values ​​to them, that is, use default values, all are optional.

2)Variable parametersFunction

<?php
     function variable()
         {
                  echo func_num_args();         //输出参数个数
                  $varArray = func_get_args;     //获取参数,返回参数数组
                   foreach($varArray as $value)
                     echo $value;
                  
                     echo func_get_arg;       //获取单个参数
                   
         }
?>
Copy after login

The above is the detailed content of Explanation of variable parameter functions and optional parameter functions in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template