I'm attempting to troubleshoot a problem, and need to understand what this if statement is saying:
我正在尝试解决问题,需要了解这个if语句的含义:
if ($confirmation = $payment_modules->confirmation()) {
All the resources I can find only show if statements with double equal signs, not single. Is this one of the shorthand forms of a php if? What is it doing?
我能找到的所有资源只显示具有双等号的语句,而不是单个。这是一个PHP的速记形式吗?它在做什么?
(If it's actually wrong syntax, changing it to a double equal sign doesn't resolve the problem. As-is, in some scenarios it does return true. In the scenario I'm troubleshooting, it doesn't return true until after I refresh the browser.)
(如果它实际上是错误的语法,将其更改为双等号并不能解决问题。原样,在某些情况下它确实返回true。在我正在排除故障的情况下,它直到我之后才会返回true刷新浏览器。)
Any help is greatly appreciated!!!
任何帮助是极大的赞赏!!!
5 个解决方案
#1
19
It's a form of shorthand, which is exactly equivalent to this:
这是一种速记形式,完全等同于:
$confirmation = $payment_modules->confirmation();
if ($confirmation) {
}
#2
8
This will first assign the value of $payment_modules->confirmation()
to $confirmation
. The =
operator will evaluate to the new value of $confirmation
.
这将首先将$ payment_modules-> confirmation()的值分配给$ confirm。 =运算符将计算$ confirmation的新值。
This has the same effect as writing:
这与写作效果相同:
$confirmation = $payment_modules->confirmation();
if ($confirmation) {
// this will get executed if $confirmation is not false, null, or zero
}
#3
4
The code works because an assignment returns the value assigned, so if $payment_modules->confirmation()
is true
, $confirmation
will be set to true
, and then the assignment will return true
. Same thing for false
.
代码有效,因为赋值返回赋值,因此如果$ payment_modules-> confirmation()为true,$ confirm将设置为true,然后赋值将返回true。同样的事情是假的。
That's why you can use a command to assign to many variables, as in a = b = 0
. Assigns zero to b
and returns that zero. Therefore, it becomes a = 0
. And a
receives zero and it will return that zero, which can or can not be used.
这就是为什么你可以使用命令分配给许多变量,如a = b = 0.将零赋给b并返回零。因此,它变为a = 0.并且a接收零并且它将返回零,其可以使用或不可以使用。
#4
0
Sometimes people like to do an assignment and then check if the assignment went through okay. Pair this up with functions that return false (or equivalent) on failure, and you can do an assignment and a check at the same time.
有时人们喜欢做作业,然后检查作业是否顺利。将此配对与失败时返回false(或等效)的函数配对,您可以同时执行赋值和检查。
In order to understand this, remember that assignments are a kind of expression, and so (like all expressions) have a return value. That return value is equal to whatever got put into the variable. That is why you can do something like
为了理解这一点,请记住赋值是一种表达式,因此(与所有表达式一样)具有返回值。返回值等于放入变量的任何值。这就是为什么你可以做类似的事情
a = b = c = 0;
a = b = c = 0;
to assign all of those variables at the same time.
同时分配所有这些变量。
#5
0
=
means assignment ( $a = 1 ), ==
is for comparison ( true == false is false ). I think in your example it should use =
because it assigns it to the return value of confirmation, which should be something that evaluates to true.
=表示赋值($ a = 1),==用于比较(true == false为false)。我认为在你的例子中它应该使用=因为它将它分配给确认的返回值,这应该是评估为true的东西。
Try doing a var_dump:
尝试做一个var_dump:
var_dump( $payment_modules->confirmation() );
See what boolean it evaluates to, and from there you can troubleshoot. Post more code if you want more help.
查看它评估的布尔值,然后从那里进行故障排除。如果您需要更多帮助,请发布更多代码。
class test() {
public function confirmation() { return true; }
}
$boolean = test::confirmation();
var_dump( $boolean );
Will equate to true
将等同于真
#1
19
It's a form of shorthand, which is exactly equivalent to this:
这是一种速记形式,完全等同于:
$confirmation = $payment_modules->confirmation();
if ($confirmation) {
}
#2
8
This will first assign the value of $payment_modules->confirmation()
to $confirmation
. The =
operator will evaluate to the new value of $confirmation
.
这将首先将$ payment_modules-> confirmation()的值分配给$ confirm。 =运算符将计算$ confirmation的新值。
This has the same effect as writing:
这与写作效果相同:
$confirmation = $payment_modules->confirmation();
if ($confirmation) {
// this will get executed if $confirmation is not false, null, or zero
}
#3
4
The code works because an assignment returns the value assigned, so if $payment_modules->confirmation()
is true
, $confirmation
will be set to true
, and then the assignment will return true
. Same thing for false
.
代码有效,因为赋值返回赋值,因此如果$ payment_modules-> confirmation()为true,$ confirm将设置为true,然后赋值将返回true。同样的事情是假的。
That's why you can use a command to assign to many variables, as in a = b = 0
. Assigns zero to b
and returns that zero. Therefore, it becomes a = 0
. And a
receives zero and it will return that zero, which can or can not be used.
这就是为什么你可以使用命令分配给许多变量,如a = b = 0.将零赋给b并返回零。因此,它变为a = 0.并且a接收零并且它将返回零,其可以使用或不可以使用。
#4
0
Sometimes people like to do an assignment and then check if the assignment went through okay. Pair this up with functions that return false (or equivalent) on failure, and you can do an assignment and a check at the same time.
有时人们喜欢做作业,然后检查作业是否顺利。将此配对与失败时返回false(或等效)的函数配对,您可以同时执行赋值和检查。
In order to understand this, remember that assignments are a kind of expression, and so (like all expressions) have a return value. That return value is equal to whatever got put into the variable. That is why you can do something like
为了理解这一点,请记住赋值是一种表达式,因此(与所有表达式一样)具有返回值。返回值等于放入变量的任何值。这就是为什么你可以做类似的事情
a = b = c = 0;
a = b = c = 0;
to assign all of those variables at the same time.
同时分配所有这些变量。
#5
0
=
means assignment ( $a = 1 ), ==
is for comparison ( true == false is false ). I think in your example it should use =
because it assigns it to the return value of confirmation, which should be something that evaluates to true.
=表示赋值($ a = 1),==用于比较(true == false为false)。我认为在你的例子中它应该使用=因为它将它分配给确认的返回值,这应该是评估为true的东西。
Try doing a var_dump:
尝试做一个var_dump:
var_dump( $payment_modules->confirmation() );
See what boolean it evaluates to, and from there you can troubleshoot. Post more code if you want more help.
查看它评估的布尔值,然后从那里进行故障排除。如果您需要更多帮助,请发布更多代码。
class test() {
public function confirmation() { return true; }
}
$boolean = test::confirmation();
var_dump( $boolean );
Will equate to true
将等同于真