0

My if statement keeps looking over one of my variables and looks at the or statements and returns true. I hate describing coding before showing it...

if($relLoc[1] <= "32" AND $map['locationpadding'] == "0px 0px -32px 0px" OR $map['locationpadding'] == "0px 0px -32px -32px" OR $map['locationpadding'] == "0px -32px -32px 0px")
{
    die();
}

So if I am at $map['locationpadding'] == "0px 0px -32px -32px" and the $relLoc[1] == "380" the die(); still executes.

But if I am at 0px 0px -32px 0px It wont execute till I am at the 32 location.

7
  • To my knowledge you can't say OR, you have to say || Commented Dec 14, 2012 at 19:08
  • In PHP OR and || is admited Commented Dec 14, 2012 at 19:08
  • I originally tried that, researched it on w3 and tried that route. Still same thing. The "OR" should be fine. Commented Dec 14, 2012 at 19:09
  • Never mind, you guys win. Thanks! But why would this one fail. if($relLoc[0] <= "32" && $map['locationpadding'] == "0px 0px -32px -32px" || $map['locationpadding'] == "0px 0px 0px -32px" || $map['locationpadding'] == "-32px 0px 0px -32px"){ die(); } Commented Dec 14, 2012 at 19:11
  • See my post to explain why that fails. Commented Dec 14, 2012 at 19:12

2 Answers 2

2

You have not correctly grouped the logic statements. I do not know PHP so the syntax may be off but you essentially want this:

if($relLoc[1] <= "32"  AND **(** $map['locationpadding'] == "0px 0px -32px 0px" OR $map['locationpadding'] == "0px 0px -32px -32px" OR $map['locationpadding'] == "0px -32px -32px 0px"**)** ){
            die();
        }

Notice the additional parenthesis to denoted the correct group of boolean statements. What you have done in your original post is this:

if relLoc == 32 AND  $map['locationpadding'] == "0px 0px -32px 0px

  +  
OR $map['locationpadding'] == "0px 0px -32px -32px"   
  + 
OR $map['locationpadding'] == "0px -32px -32px 0px

So in the sample you provided it would be this:

$map['locationpadding'] == "0px 0px -32px -32px" and the $relLoc[1] == "380" 

which is:

   False + True + False = True
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! Thanks for the help. All for parenthesis! Works great now!
@Ajezior great, common courtesy is to upvote / accept if this is the correct solution.
0

Looks like you want:

if($relLoc[1] <= "32" AND ($map['locationpadding'] == "0px 0px -32px 0px" OR $map['locationpadding'] == "0px 0px -32px -32px" OR $map['locationpadding'] == "0px -32px -32px 0px")){
//                        ^ added                                                                                                                                                ^ added

Use parenthesis to separate sub conditions, otherwise your first condition becomes less relevant because after that you have OR x OR x OR x, so only one of the ORs has to evauluate to true for the whole condition to evaluate to true.

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.