0

This is for a text field password input. Here is my code:

$vaar is what is entered into that field.

if(($vaar !='pass01' or 'pass02') && (!empty($vaar))) {

I can't figure out the correct syntax. I've tried ||, or, xor. I've also tried placing 'pass01' and 'pass02' in their own ( )'s.

What I want it to do is this:

If $vaar isn't 'pass01' or 'pass02' and $vaar is not empty then do this:

just a syntax error, but I can't figure it out!

1
  • Besides the good answers below, I want to add something general: PHP checks the expressions from left to right, if one fails completely (without an alternative expression evaluateing true by OR) it won't check the following anymore Commented Jul 11, 2013 at 19:41

2 Answers 2

4

You're looking for:

if( !empty($vaar) && ($vaar != 'pass01' && $vaar != 'pass02')) {

Note that I've put the empty() check first, which will short-circuit the evaluation should $vaar in fact be empty.

Edit: To better reflect the OP's wording (and logic), this is identical to the following, because of Demorgan's law.):

if( !empty($vaar) && !($vaar == 'pass01' || $vaar == 'pass02')) {

Reads: If $vaar is not empty and $vaar is not pass01 or pass02

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

5 Comments

x != y || x != z for differing y and z always returns true. -1
@Kolink - You're absolutely right, I had originally written it with && and then changed it from reading the OP, but the correct logic expression is with &&. Thanks.
@Downvoters - Care to explain? I would like to improve my answer if it is inadequate.
@nickb I'm getting downvoted too but I think your answer is adequate.
both of you two ended up with the same answer, and it works. Points go here b/c he posted the empty() in the beginning, and also for the DeMorgan's law reference :-)
1
if(($vaar !== 'pass01' and $vaar !== 'pass02') and (!empty($vaar))) {
   // ...
}

Alternatively:

if(($vaar !== 'pass01' && $vaar !== 'pass02') && (!empty($vaar))) {
   // ...
}

What you actually mean is:

If $vaar is not equal to "pass01" AND $vaar is not equal to "pass02" AND $vaar is not empty, then ...

Note that I have kept your original parentheses in there, but since they are all AND, you may remove them, like this:

if(!empty($vaar) && $vaar !== 'pass01' && $vaar !== 'pass02') {
   // ...
}

Here, I have also moved the empty language construct to the beginning of the conditional, as if the variable is empty then the conditional will evaluate the false immediately, preventing an E_NOTICE error for Undefined variable $vaar if the variable is undefined.

2 Comments

Suggest moving the empty check first, since that will short-circut the other two conditions (as it is more broad) but will also prevent notices in case of an undefined variable.
@Kolink Good idea, answer now improved.

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.