我不确定究竟是什么类型的参数列表行为被调用,因此很难搜索更多信息。我将举一个例子:
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语句,即使变量中的值被正确映射(我认为)。
答案 0 :(得分:0)
如果你有
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.
答案 1 :(得分:0)
您所看到的内容取决于函数参数的顺序,而不是变量名称。如果您的函数定义是
function echoThis( $a=NULL, $b=NULL, $c=NULL ) {
...
}
您将其称为
echoThis($a);
然后你会看到你期望的行为。但是,如果你打电话
echoThis($b);
然后参数(在这种情况下为$b
)将映射到函数的第一个参数,在执行函数时将其称为变量$a
。要达到预期的效果,您必须致电
echoThis (null, $b)
这将有效地忽略第一个参数。
有关详细信息,请参阅php.net/manual/en/functions.arguments.php。
答案 2 :(得分:0)
这是应该的:
--------
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
不为空,它首先被回显,其余的则没有。在所有这些中(this
和that
)A
不是空的。如果它根据名称将它们分配给适当的变量,您会看到B
不为空或C
不为空。