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.
-
1Please clarify the reason(s) for the downvote. The question was clear and I did several searches to no avail before posting. I noticed that some small grammatical edits were made - thank you.Joel– Joel2018年12月08日 18:27:35 +00:00Commented Dec 8, 2018 at 18:27
-
I'll upvote you because I agree; there's nothing wrong with this question.Lawrence Johnson– Lawrence Johnson2018年12月08日 18:53:29 +00:00Commented Dec 8, 2018 at 18:53
2 Answers 2
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
1 Comment
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.