I want to manually invert a logical condition.
Let's say we have this:
$a = true;
$b = false;
if (!$a or $b) {
echo 'true';
}
if (!(!$a or $b)) { //I want to solve !(!$a or $b) to a form without ()
echo 'true 2';
}
What are the rules?
I want to manually invert a logical condition.
Let's say we have this:
$a = true;
$b = false;
if (!$a or $b) {
echo 'true';
}
if (!(!$a or $b)) { //I want to solve !(!$a or $b) to a form without ()
echo 'true 2';
}
What are the rules?
So let say
!(!$a or $b) === true
is the same as !$a or $b === false
.
So !$a or $b
must be false, which means !$a
and $b
should be both false, which in turn means $a
must be true and $b
must be false.
In the end, this !(!$a or $b)
is equivalent to $a and !$b
.
$a $b !(!$a or $b) $a and !$b
T T F F
T F T T
F T F F
F F F F
We can also prove this using Boolean algebra and De Morgan’s theorem
¬(¬A∨B) = ¬(¬A)∧¬B = A∧¬B
According to De Morgan's laws:
!(A and B) == !A or !B
!(A or B) == !A and !B
Therefore
!(!$a or $b) == !!$a and !$b == $a and !$b
You can also draw up an evaluation table:
$a $b !(!$a or $b) $a and !$b
true true false false
true false true true
false true false false
false true false false
Side note:
Careful with and
/or
when you have assignments like $val = true and false;
, cf. 'AND' vs '&&' as operator