MakeWebGames

# Logic twister

## Recommended Posts

I ran across an interesting bit of code that I think some folks might appreciate.

```<?php
\$blah = 1;
\$foo = 1;
if (!isset(\$blah) xor isset(\$foo)) {
echo 'only one variable may be set...';
} else {
echo 'yay, only one variable is set';
}```

If you test that out, try commenting out one of the two variables at the beginning. Then uncomment it and comment the other. Lastly, comment them both, and see the results.

To bring home what the logic is that we have going on here, this next bit of code is another way to write that conditional statement.

```<?php
\$blah = 1;
\$foo = 1;
if ( (isset(\$foo) and isset(\$blah) or (!isset(\$foo) and !isset(\$blah)) {
echo 'only one variable may be set...';
} else {
echo 'yay, only one variable is set';
}```

So as you can see, the first one twists up the "isset() and !isset()" using the "xor" logical operator and is a bit shorter.

##### Share on other sites

Re: Logic twister

What Floydian has stumbled upon is none other than the basics of boolean logic - named after George Boole, who in the mid 19th centurfy defined this simple algebraic system.

Programmers have been using these concepts since the very early days of computing as the simple operations AND, OR and XOR are ideally suited to binary systems.

Using truth tables perhaps gives a slightly clearer representation of Floydians example above:

AND table

```+---+---+---+
| A | B | X |
+---+---+---+
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
+---+---+---+```

OR table

```+---+---+---+
| A | B | X |
+---+---+---+
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
+---+---+---+```

XOR table

```+---+---+---+
| A | B | X |
+---+---+---+
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
+---+---+---+```

So you can see for each relevant operation:

AND -- only true when ALL the inputs are set

OR -- true when ANY of the inputs are set

XOR -- true when only one of the inputs are set

##### Share on other sites

Re: Logic twister

You missed the point, but you never miss an opportunity to be condescending.

Well, you did something. :S

So, the point here, and it's not a big deal, and it's not a Californian gold rush, is interesting nonetheless.

To test if only one of two variables is set, you can test if one is set, and the other isn't using the xor operator instead of testing if both exist or if both don't exist. It's something I'm willing to bet a lot of people never thought about even if they did RTFM.

;)

So, please keep the condescension to yourselves folks if you have nothing better to contribute to the community, EVEN IF YOU'RE AN ADMIN. :O

##### Share on other sites

• 3 weeks later...

Re: Logic twister

Um Ok Nyna, lol

Wierd post..I hate history

Very useful Floydian

I'll be using this quite soon.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×

• #### Marketplace

• Discord Chat
• PBBG Directory
×
• Create New...