Reputation: 124
I found an example php assignment statement online which maybe resembles a tertary conditional condensed statement, but not quite. Does anyone have insight as to how to read this assignment statement?
$access = $access || $note->uid == $user->uid && user_access('note resource view own notes');
My first guess was "assign to access whatever is in access, or if empty, whether uid values are equal and'd with the return of user_access." But I get the feeling that is not correct, as it seems illogical.
Upvotes: 1
Views: 56
Reputation: 11816
First have a look at the Operator Precedence
==
comes before &&
comes before ||
comes before =
Thus your statement is more clear with adding the following parentheses:
$access = (
$access
||
(
($note->uid == $user->uid)
&&
user_access('note')
)
);
assign to access whatever is in access, or if empty,
Not quite: assign to $access
the value true
* when $access
already evaluates to true
(true
, 1
, "some string"
etc), or
whether uid values are equal and'd with the return of user_access
Correct
And otherwise assign false
. After this statement $access
is always either true
or false
, even when $access === 'yes'
before.
Note*: ||
and &&
are boolean operators, only capable of 'returning' true
or false
Upvotes: 1
Reputation: 1107
I had this exact type of statement in a library way back, and it's basically an elaborate (or maybe just badly-styled?) null-check. Because PHP uses short circuit evaluation, the right-hand side of the or-expression will not evaluate if the left hand one was null.
Upvotes: 1