我不确定这种参数列表行为到底叫什么,所以很难搜索更多信息。我举个例子:
function echoThis( $a=NULL, $b=NULL, $c=NULL )
{
if( $a != NULL )
echo "A is not NULL; it is: " . $a . "'n";
if( $b != NULL )
echo "B is not NULL; it is: " . $b . "'n";
if( $c != NULL )
echo "C is not NULL; it is: " . $c . "'n";
}
function echoThat( $a=NULL, $b=NULL, $c=NULL )
{
if( $a != NULL )
echo "A is not NULL; it is: " . $a . "'n";
elseif( $b != NULL )
echo "B is not NULL; it is: " . $b . "'n";
elseif( $c != NULL )
echo "C is not NULL; it is: " . $c . "'n";
else
echo "Huh?'n";
}
$a = "hello";
$b = "how are you";
$c = "goodbye";
echoThis( $a );
echoThis( $a, $b );
echoThis( $a, $b, $c );
echo "--------'n";
echoThat( $a );
echoThat( $a, $b );
echoThat( $a, $b, $c );
echo "--------'n";
echoThis( $a );
echoThis( $b, $a );
echoThis( $c, $b, $a );
echo "--------'n";
echoThat( $a );
echoThat( $b, $a );
echoThat( $c, $b, $a );
echo "--------'n";
echoThis( $a );
echoThis( $b );
echoThis( $c );
echo "--------'n";
echoThat( $a );
echoThat( $b );
echoThat( $c );
?>
因此,无论传递的变量的顺序如何,它都会填充内部作用域变量,就好像第一个是$a的,第二个是$b和最后一个$c,但如果只提供一个参数,它会以某种方式将变量名称与正确的内部作用域变量匹配。因此,如果只传递$c,它会跳过$a并$b,并知道将其分配给$c。
A is not NULL; it is: hello
A is not NULL; it is: hello
B is not NULL; it is: how are you
A is not NULL; it is: hello
B is not NULL; it is: how are you
C is not NULL; it is: goodbye
--------
A is not NULL; it is: hello
A is not NULL; it is: hello
A is not NULL; it is: hello
--------
A is not NULL; it is: hello
A is not NULL; it is: how are you
B is not NULL; it is: hello
A is not NULL; it is: goodbye
B is not NULL; it is: how are you
C is not NULL; it is: hello
--------
A is not NULL; it is: hello
A is not NULL; it is: how are you
A is not NULL; it is: goodbye
--------
A is not NULL; it is: hello
A is not NULL; it is: how are you
A is not NULL; it is: goodbye
--------
A is not NULL; it is: hello
A is not NULL; it is: how are you
A is not NULL; it is: goodbye
这叫什么,以便我可以找到更多信息?(或者这被认为是一种不好的做法?对我来说最令人讨厌的是中间的两节产生不同的结果,最后两节都使用第一个 if 语句触发,即使变量中的值也被正确映射(我认为(。
如果你有
function say($first_argument = null, $second_argument = null) {
echo $first_argument;
echo $second_argument;
}
那么在该函数之外调用什么变量并不重要。 你可以有
$random_variable = "hello";
$other_variable = " world";
say($random_variable, $other_variable);
//outputs "hello world"
这与
say("hello", " world");
如果您希望能够通过变量最初的名称来引用变量,那么您真的不能。 你可以传入一个数组:
function say($args) {
extract($args);
echo $a;
echo $b;
}
$args = array('a' => 'hello', 'b' => ' world');
say($args); //hello world
这将允许您通过数组中的键引用变量,在这种情况下,它将匹配它们。 例如:
$args = array('b' => ' world', 'a' => ' hello');
say($args); //hello world.
这是它应该工作:
--------
this-a
A is not NULL; it is: hello
--------
this-ab
A is not NULL; it is: hello
B is not NULL; it is: how are you
--------
this-abc
A is not NULL; it is: hello
B is not NULL; it is: how are you
C is not NULL; it is: goodbye
--------
that-a
A is not NULL; it is: hello
--------
that-ab
A is not NULL; it is: hello
--------
that-abc
A is not NULL; it is: hello
--------
this-a
A is not NULL; it is: hello
--------
this-ba
A is not NULL; it is: how are you
B is not NULL; it is: hello
--------
this-cba
A is not NULL; it is: goodbye
B is not NULL; it is: how are you
C is not NULL; it is: hello
--------
that-a
A is not NULL; it is: hello
--------
that-ba
A is not NULL; it is: how are you
--------
that-cba
A is not NULL; it is: goodbye
--------
this-a
A is not NULL; it is: hello
--------
this-b
A is not NULL; it is: how are you
--------
this-c
A is not NULL; it is: goodbye
--------
that-a
A is not NULL; it is: hello
--------
that-b
A is not NULL; it is: how are you
--------
that-c
A is not NULL; it is: goodbye
每个函数都被正确调用,没有陌生感。 传递的第一个参数将始终$a
,第二个参数将始终$b
,第三个参数将始终$c
。
在this-cba
A
变成再见而不是你好。
在that-cba
A
成为再见而不是你好。
在that-b
A
变得how are you
. $a
不为 null,它首先被回显出来,其余的则不为空。 在所有这些最后的(this
和that
(中,A
不是空的。 如果根据名称将它们分配给正确的变量,您将看到B
不为空或C
不为空。
您看到的内容取决于函数参数的顺序,而不是变量名称。如果函数定义为
function echoThis( $a=NULL, $b=NULL, $c=NULL ) {
...
}
你称之为
echoThis($a);
然后你会看到你期望的行为。但是,如果您要致电
echoThis($b);
然后参数(在本例中为 $b
(将映射到函数的第一个参数,该参数在执行函数时被引用为变量$a
。为了达到您期望的结果,您必须致电
echoThis (null, $b)
这将有效地忽略第一个参数。
有关详细信息,请参阅 php.net/manual/en/functions.arguments.php。