tags:

views:

179

answers:

5

I'm attempting to troubleshoot a problem, and need to understand what this if statement is saying:

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?

(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.)

Any help is greatly appreciated!!!

A: 

= 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.

Try doing a 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

meder
The var_dump idea helped too. It's null before the refresh, and filled with array contents after the refresh. Thanks for your super quick response and the offer of more help. Let me take this info and run with it a little more first.
FrustratedPHPnovice
+6  A: 

It's a form of shorthand, which is exactly equivalent to this:

$confirmation = $payment_modules->confirmation();
if ($confirmation) {

}
nickf
Perfect, thanks! That I can understand! Now to just figure out why $payment_modules->confirmation() is empty until after I refresh the browser...
FrustratedPHPnovice
@FrustratedPHPnovice click the checkmark next to this answer to accept it
Mike B
yeah! ;) hehehe
nickf
A: 

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.

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;

to assign all of those variables at the same time.

Platinum Azure
+5  A: 

This will first assign the value of $payment_modules->confirmation() to $confirmation. The = operator will evaluate to the new value of $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
}
Joel
This is more of a meta-observation, but isn't it interesting when someone with lower rep has fewer up-votes than someone with higher rep even though the answers are almost identical and the lower-rep person answered first? Interesting social phenomenon is all. +1 for having a clear explanation along with that code.
mrduclaw
+4  A: 

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.

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.

jpmelos