Reputation: 5706
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.
Upvotes: 1
Views: 10574
Reputation: 2500
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.';
}
Upvotes: 6
Reputation: 3541
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.
Upvotes: 1
Reputation: 1676
$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';
}
Upvotes: 0
Reputation: 312
$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.';
}
Upvotes: 2
Reputation: 140
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"
Upvotes: 2