你如何在 php 中创建可选参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you create optional arguments in php?
提问by gregh
In the PHP manual, to show the syntax for functions with optional parameters, they use brackets around each set of dependent optional parameter. For example, for the date()function, the manual reads:
在 PHP 手册中,为了显示带有可选参数的函数的语法,他们在每组相关可选参数周围使用括号。例如,对于date()函数,手册上写着:
string date ( string $format [, int $timestamp = time() ] )
Where $timestampis an optional parameter, and when left blank it defaults to the time()function's return value.
where$timestamp是一个可选参数,当留空时默认为time()函数的返回值。
How do you go about creating optional parameters like this when defining a custom function in PHP?
在 PHP 中定义自定义函数时,您如何创建这样的可选参数?
回答by Jeff Winkworth
Much like the manual, use an equals (=) sign in your definition of the parameters:
与手册非常相似=,在参数定义中使用等号 ( ):
function dosomething($var1, $var2, $var3 = 'somevalue'){
// Rest of function here...
}
回答by Ross
The default value of the argument must be a constant expression. It can't be a variable or a function call.
参数的默认值必须是常量表达式。它不能是变量或函数调用。
If you need this functionality however:
但是,如果您需要此功能:
function foo($foo, $bar = false)
{
if(!$bar)
{
$bar = $foo;
}
}
Assuming $barisn't expected to be a boolean of course.
假设$bar当然不会是布尔值。
回答by gregh
Some notes that I also found useful:
一些我也觉得有用的笔记:
Keep your default values on the right side.
function whatever($var1, $var2, $var3="constant", $var4="another")The default value of the argument must be a constant expression. It can't be a variable or a function call.
保留右侧的默认值。
function whatever($var1, $var2, $var3="constant", $var4="another")参数的默认值必须是常量表达式。它不能是变量或函数调用。
回答by mk.
Give the optional argument a default value.
给可选参数一个默认值。
function date ($format, $timestamp='') {
}
回答by Lars Gyrup Brink Nielsen
The date function would be defined something like this:
日期函数的定义如下:
function date($format, $timestamp = null)
{
if ($timestamp === null) {
$timestamp = time();
}
// Format the timestamp according to $format
}
Usually, you would put the default value like this:
通常,您会像这样放置默认值:
function foo($required, $optional = 42)
{
// This function can be passed one or more arguments
}
However, only literalsare valid default arguments, which is why I used nullas default argument in the first example, not$timestamp = time(), and combined it with a null check. Literals include arrays (array()or []), booleans, numbers, strings, and null.
但是,只有文字是有效的默认参数,这就是为什么我null在第一个示例中将其用作默认参数,而不是$timestamp = time(),并将其与空检查结合使用。文字包括数组(array()或[])、布尔值、数字、字符串和null.
回答by Gergely Lukacsy
If you don't know how many attributes need to be processed, you can use the variadic argument list token(...) introduced in PHP 5.6 (see full documentation here).
如果您不知道需要处理多少个属性,您可以使用...PHP 5.6 中引入的可变参数列表 token( )(请参阅此处的完整文档)。
Syntax:
句法:
function <functionName> ([<type> ]...<$paramName>) {}
For example:
例如:
function someVariadricFunc(...$arguments) {
foreach ($arguments as $arg) {
// do some stuff with $arg...
}
}
someVariadricFunc(); // an empty array going to be passed
someVariadricFunc('apple'); // provides a one-element array
someVariadricFunc('apple', 'pear', 'orange', 'banana');
As you can see, this token basically turns all parameters to an array, which you can process in any way you like.
如您所见,此令牌基本上将所有参数转换为一个数组,您可以按照自己喜欢的任何方式对其进行处理。

