Posts Tagged ‘logic’

NAND and NOR

13. August 2009

No Comments »

In addition to the logical operations defined here – computer scientists have defined two other operations (which are merely abstractions of operations defined in my previous article) since they are used so often. They are NAND and NOR, symbolically ↑ (|) and ↓ (⊥). I say they are abstractions because they are simply the negation of AND and OR operations

NAND

A B A AND B NOT (A AND B) A NAND B
1 1 1 0 0
1 0 0 1 1
0 1 0 1 1
0 0 0 1 1

NOR

A B A OR B NOT (A OR B) A NOR B
1 1 1 0 0
1 0 1 0 0
0 1 1 0 0
0 0 0 1 1

Illogical PHP Logic

17. July 2009

No Comments »

In algebra, after we learn the basic distributive, commutative, and associative properties, the transitive property of equality is usually next in the curriculum. For those of you who do not recall the terminology, the transitive property of equality says if $a == $b and $b == $n then $a == $n. Using this age old logic, you can prove FALSE == TRUE and 0 == 1 in PHP. Here is how:


$a = 0;
$b = "Hooray for PHP logic and dynamic type casting?";
var_dump(((FALSE == $a) == ($a == $b)) == ($b == TRUE));
var_dump((((0 == $a) == ($a == $b)) == ($b == TRUE) == (TRUE == 1));

Returns
bool(true)
bool(true)

Q.E.D

Note: I am comparing values not types. That being said, forgive me when I flame you for using PHP inappropriately. A string is NOT an integer and an integer is NOT a boolean. They should not be used as such.