2

I would like to set the disabled state of a form field based on the combination of 4 variables: processed, process started, process ended, user id

If it is not going to be processed, the form field should be disabled

If the process has started OR ended, it should be also disabled, except if the user id == 1. So User 1 can still fill the form field, even if the process has started OR ended. And it should be also disabled for User 1 also if it is not going to be processed.

I was trying this way, but doesn't work as I expect, so there must be a flaw in my logic or understanding how PHP works:

'disabled' => !$proc || (($proc_started || $proc_ended) && !$user_id == 1)

This way other users see the form field also enabled, which I don't want. Is it the hierarchy of the Logical Operators ? Can you please point me to the right direction? Thanks.

2
  • 4
    ! has a pretty high precedence, so you probably want ... && $user_id !== 1 instead. See php.net/manual/en/language.operators.precedence.php Commented Jun 25, 2019 at 9:12
  • This is the best advice. Could you please add as an answer so that I can accept? Thank you very much! Commented Jun 25, 2019 at 9:18

2 Answers 2

4

!$user_id == 1 is (!$user_id) == 1

$foo = 42;

!$foo == false;

You want to write !($user_id == 1) or $user_id != 1

Sign up to request clarification or add additional context in comments.

4 Comments

Is $user_id != 1 the same as $user_id !== 1 ? Because then your answer is also correct.
=== and the negative version !== check for the type too. 1 == "1" is true and 1 === "1" is false, 1 != "1" is false and 1 !== "1" is true
You are probably right, but still it seems too complicated for me... :) You've meant: !($user_id == 1) or $user_id !== 1, right? I think !($user_id == 1) is OK, but $user_id != 1 is not, is it?
!($user_id === 1) is equivalent to ($user_id !== 1). The first one is not $user_id equal to 1 the second is $user_id not equal to 1. !(1 === 1) is false, 1 !== 1 is false too. !(42 === 1) is true, 42 !== 1 is true too
2

Should work.

if($user_id === 1) {

    if($state != "processed") {

        $state = "Enabled" // or anything else of your choice

    }

} else {

    $state = "Disabled";

}

Comments

Your Answer

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.