1

Here is my statement:

if($row_x['xfield']==$_POST['x1'] AND $row_x['yfield']==$_POST['y'])
{
   echo "ok";
}
else {
   echo "invalid";
}

When both are not equal to each other, it must display 'invalid' else, 'ok'. But in this case it displays 'invalid' whether it is valid or not.

Any clues to why?

13
  • 4
    what output of echo $row_x['xfield']. '==' . $_POST['x1'];? Commented Sep 25, 2012 at 8:25
  • 3
    @LewsTherin AND and OR are able to be used in as operators, but && and || have a higher order of priority. Commented Sep 25, 2012 at 8:26
  • 1
    @Jean The syntax looks fine, short of funky data that you would have to validate, I simply cannot see anything that visibly stands out as wrong. Commented Sep 25, 2012 at 8:28
  • 1
    @Jean well if output can be anything then error can be anywhere Commented Sep 25, 2012 at 8:29
  • 2
    are you sure it is x1? Commented Sep 25, 2012 at 8:36

4 Answers 4

2

You said 'when BOTH are NOT equal'... implying you would want to use '!=' instead of '==' in your comparisons. See this T/F chart for &&:

--------------
| X | && | Y |
--------------
| F | F  | F |
--------------
| F | F  | T |
--------------
| T | F  | F |
--------------
| T | T  | T |
--------------

The X statement must be true AND the Y statement must be true. In your example

$row_x['xfield']==$_POST['x1']

must be TRUE as well as

$row_x['yfield']==$_POST['y']

But if I read your question you will want to have both statements as FALSE to show 'invalid'. So you'll need to substitute '==' for '!=' in each statement.

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

4 Comments

Using the logic in the question, if unless both conditions are met, it should drop down into the the else clause.
His statement is correct: "When both are not equal to each other, it must display 'invalid' "
Read his code and what he needs again. Everything is correct. You just rephrasing the same thing OP already has
"When both are not equal to each other, it must display 'invalid', else 'ok'" he states, implying either an OR comparison or his && comparison inverted, right? * getting confused*
1

Replace AND with OR and it'll show ok when at least one is equal

Comments

1

its because the any of them is not true or both

you are using the &&/AND so both must be match since you are getting the else result it mean any of them is not equal or both

you can check this by below method

if($row_x['xfield']==$_POST['x1']){
   echo ' xfiekd  is = to x1';
   if($row_x['yfield']==$_POST['y']){
     echo ' yfiekd  is = to y and aslo 1 is true';
   }else{ echo 'y is false' }


}
else{
   echo ' xfiekd  is = to x1' is false;
   if($row_x['yfield']==$_POST['y']){
     echo ' yfiekd  is = to y and aslo 1 is true';
   }else{ echo 'y is false' }


}

to get in if function body on true at least one condition you need to use the || instead of the AND

difference betwen AND an OR

truth table for AND

  a    b

true  true   = true
true  false  = false
false  false = false
false  true  = false

truth table for OR

  a    b

true  true   = true
true  false  = true
false true   = true
false false  = true
true  false  = true

1 Comment

@hayden check this now .. is it ok or should i need to elaborate more
0
if($row_x['xfield']==$_POST['x1'] OR $row_x['yfield']==$_POST['y'])
{
   echo "ok";
}
else {
   echo "invalid";
}

alternative

if($row_x['xfield']!=$_POST['x1'] AND $row_x['yfield']!=$_POST['y'])
{
   echo "ok";
}
else {
   echo "invalid";
}

1 Comment

we can not use the OR as a substitute of AND since OP wants to check both

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.