1

I have a confusion, what is wrong here? if A & B or C is set it should print yes, otherwise it should print the error message

$a = 1;
$b = 2;
$c = null;

if ((is_null($a) && is_null($b)) || is_null($c)) {
    echo 'A and B or C cannot be blank.';
}
else
    echo 'yes';

here i have A & B assigned but still its printing 'A and B or C cannot be blank.'

To Make it more clear. A= Firstname, B=lastName, C=Email. so user should either give first name and last name otherwise email.

4
  • 3
    The logic is currently: if $a AND $b is NULL, OR if $c is NULL, enter. It's doing what it's suppose to. Commented Oct 1, 2015 at 10:06
  • @Epodax but in my acse $a AND $b are set to 1 and 2. they are not null. Commented Oct 1, 2015 at 10:10
  • The entire if clause is read/checked, not just the first part. Commented Oct 1, 2015 at 10:12
  • i updated the question to make it more clear Commented Oct 1, 2015 at 10:14

5 Answers 5

6

You should do this following way:

$a = 1;
$b = 2;
$c = null;

if ((!is_null($a) && !is_null($b)) || !is_null($c)) {
    echo 'yes';
}
else
{
    echo 'A and B or C cannot be blank.';
}

Update:

if ((is_null($a) || is_null($b)) && is_null($c)) {
    echo 'A and B or C cannot be blank.';
}
Sign up to request clarification or add additional context in comments.

2 Comments

its failing when i assigned these. $a = null; $b = 1; $c = 3; i have defined c, so it should say yes but its saying the error.
can u write this using only if without else. if condition then echo error message. thats all.
2

You have A&B assigned but the if condition says that A&B or C must be true to print "A and B or C cannot be blank". The A&B part are false at the moment you assigned value to them. This means if( false (A&B assigned) or true (C is not assigned)) which will lead to true and thus print "A and B or C cannot be blank"

1 Comment

If I were you I would switch what you want to echo. In the if statement echo "yes" and in the else statement echo "A and B or C cannot be blank". Second option is to change all is_null into !is_null and don't switch what is echoed.
2
    $a = 1;
    $b = 2;
    $c = null;

    if ((is_null($a) && is_null($b)) || is_null($c)) {
        echo 'yes';
    }
   else{    
     echo 'A and B or C cannot be blank.';
    }

Comments

1

In your original post, you wrote

if A & B or C is set it should print yes, otherwise it should print the error message

but you are actually testing for unset variables.

A solution closer to your assertion (and also more readable) would be to write the code like this:

if (($a && $b) || $c) {
    echo 'yes';
} else {
    echo 'A and B or C cannot be blank.';
}

The brackets around $a && $b are not necessary, but help determining the expected groupings.

Comments

0
    $a = 1;
    $b = 2;
    $c = null;

    if ((empty($a) && empty($b)) || empty($c)) {
         echo 'A and B or C cannot be blank.';
    }
   else{    
      echo 'yes';
    }

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.