0

I'm trying to create an if() statement to catch any unwanted tampering or coding errors.

All parts are strings.

I've tried correct and incorrect values in the array, but it seams to default to true every time.. outputting the error.

if ($array['field'] !== ('b' || 'c')){
    echo 'An error has been encounterd <br/>';
    exit();
}

Any ideas? thanks

3 Answers 3

5

You're comparing $array['field'] to the result of 'b' || 'c'.

You need to compare it to each of the things you want to match against.

You have to say "If the thing is not 'b' and it is also not 'c'".

if ($array['field'] !== 'b' && $array['field'] !== 'c'){
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant! Big green tick is coming your way when the system allows
2

try, this way:

if ($array['field'] !== 'b' && $array['field'] !==  'c'){

1 Comment

That's never going to be true. That shouldn't be an ||.
1
if (!in_array($array['field'], array('b', 'c'))) {
    // ...
}

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.