Here is the sample:
if(($test = array('key'=>true)) && $test['key']){
// works
}
if($test = array('key'=>true) && $test['key']){
// does not work
}
Why is the parenthesis required? My understanding is that it computes the first conditional then the second no matter what.
And is it “safe” to do an assignment like this?
It’s a matter of operator precedence in the language. In your second statement, you’re essentially writing this to be evaluated:
Just about any language is going to take that to mean this:
It’s assigning to
$testthe value of the evaluation of the logical&&between the two other values. So$testwill be eithertrueorfalsewhen evaluated.