1

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.

rpm192
2,4593 gold badges22 silver badges39 bronze badges
asked Dec 8, 2018 at 17:43
2
  • 1
    Please 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. Commented Dec 8, 2018 at 18:27
  • I'll upvote you because I agree; there's nothing wrong with this question. Commented Dec 8, 2018 at 18:53

2 Answers 2

1

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

answered Dec 8, 2018 at 18:01

1 Comment

Very good clarity. Thanks for the insight. Hadn't thought about analyzing operator precedence.
1

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.

answered Dec 8, 2018 at 17:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.