Php 定义函数参数2并将参数1保留为默认值

Php 定义函数参数2并将参数1保留为默认值,php,Php,如果您有以下功能 function func1($captainObvious="John",$name){ return "Hallo my name is ".$captainObvious."<br>Welcome ".$name."!"; } 但我似乎找不到这种可能性。在创建函数时,应该先放置所需的参数,然后再将带有默认值的参数放在后面。否则,您会遇到这样的情况,您现在必须为第一个参数提供一个值。在创建函数时,您应该首先放置所需的参数,然后将带有默认值的参数放在后面。否

如果您有以下功能

function func1($captainObvious="John",$name){
  return "Hallo my name is ".$captainObvious."<br>Welcome ".$name."!";
}

但我似乎找不到这种可能性。

在创建函数时,应该先放置所需的参数,然后再将带有默认值的参数放在后面。否则,您会遇到这样的情况,您现在必须为第一个参数提供一个值。

在创建函数时,您应该首先放置所需的参数,然后将带有默认值的参数放在后面。否则,您将遇到这样的情况,您现在必须为第一个参数提供一个值。

如果无法更改
func1
,则必须使用
func1(“John”、“Benny”)
。如果您可以对其进行更改,但由于遗留问题而无法更改参数顺序(首选解决方案!),则可以使用
null
作为默认标志:

function func1($captainObvious="John", $name){
  if ($captainObvious === null) {
    $captaionObvious = "John";
  }
  return "Hallo my name is ".$captainObvious."<br>Welcome ".$name."!";
}

func1(null,"Benny");
函数func1($captainOutsible=“John”,$name){
如果($captainOperature==null){
$captaionovious=“John”;
}
return“你好,我的名字是“$captain显而易见。”
欢迎“$name!”!”; } func1(空,本尼);
如果无法更改
func1
,则必须使用
func1(“约翰”、“本尼”)
。如果您可以对其进行更改,但由于遗留问题而无法更改参数顺序(首选解决方案!),则可以使用
null
作为默认标志:

function func1($captainObvious="John", $name){
  if ($captainObvious === null) {
    $captaionObvious = "John";
  }
  return "Hallo my name is ".$captainObvious."<br>Welcome ".$name."!";
}

func1(null,"Benny");
函数func1($captainOutsible=“John”,$name){
如果($captainOperature==null){
$captaionovious=“John”;
}
return“你好,我的名字是“$captain显而易见。”
欢迎“$name!”!”; } func1(空,本尼);
您必须首先输入所需的变量

比如说

Function func1($captainObvious="John",$name){
    return "Hallo my name is ".$captainObvious."<br>Welcome ".$name."!";
}

Function func2($name, $captainObvious="John"){
    return "Hallo my name is ".$captainObvious."<br>Welcome ".$name."!";
}
将为功能1付出代价

你好,我的名字是

欢迎你,本尼

和功能2

你好,我叫约翰

欢迎你,本尼


您必须首先放置所需的变量

比如说

Function func1($captainObvious="John",$name){
    return "Hallo my name is ".$captainObvious."<br>Welcome ".$name."!";
}

Function func2($name, $captainObvious="John"){
    return "Hallo my name is ".$captainObvious."<br>Welcome ".$name."!";
}
将为功能1付出代价

你好,我的名字是

欢迎你,本尼

和功能2

你好,我叫约翰

欢迎你,本尼


为什么不切换参数的顺序?为什么不切换参数的顺序?