1

I have the following test on some data:

if(isset($option_type)
    && ($option_type == 3 || $option_type == 4
                && (isset($ext_data) && $ext_data != "")
                )
  ){}

What it should do, is if $option type is NOT 3 or 4, the test should return true. If $option type is 3 or 4 AND $ext_data does not exist or equals "", then the test should return false.

However, no matter what $option_type is, it returns false.

How can I format this test so when the $option_type is 3 or 4 and $ext_data exists and is not "", the result is true?

lee

5
  • 3
    a quick suggestion here, separate them out and return them, see what they return (true or false), then organize the order from there Commented Jul 27, 2012 at 2:58
  • 1
    Two words: operator precedence. It might not immediately fix your test, but the current code is most likely not what you want. Commented Jul 27, 2012 at 3:12
  • I know a little about operator precedence. However, I'm not sure that applies here since all the outer testing uses && and the inner testing is parenthesized. Is this correct or am I missing something? Commented Jul 27, 2012 at 3:16
  • The && on your third line only applies if $option_type == 4. Due to lazy logic, if $option_type == 3 the code won't even bother computing the third line. Commented Jul 27, 2012 at 3:19
  • Thanks Palladium. I just did a bunch of reading and understand a little more. I still am not clear exactly why my test is not working, but I got some new knowledge for future use. Commented Jul 27, 2012 at 3:25

2 Answers 2

2

I would split the tests into two to make it clearer:

if (!isset($ext_data) || $ext_data == "") return false;
return $option_type != 3 && $option_type != 4;
Sign up to request clarification or add additional context in comments.

1 Comment

Hello, I can do this, no problem. But I am curious to know why the test as it is does not work or what is not clear about it. I've used similar testing in ActionScript and C# with no problem. Is it a PHP thing? Is it just incorrect?
0

//try this

if(isset($option_type)&& ($option_type == 3 || $option_type == 4)){
    if (isset($ext_data) && $ext_data == ""){
            return false;
    }else{
        return 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.