PHP LogicSeptember.9.2008
Today in geometry we were doing logic. I'll give you an example:
p: Today is Tuesday.
q: 2 + 7 = 9
Note that you must have p and q. So, would statement p be true? Yes, it would. However, what if I asked that to you yesterday? Then it would be false. Statement q on the other hand is always true because there is no way to not make that statement false without changing it.
We then moved onto combining two statements with and. Using the same examples as above, what would the following case (two statements combined with and) result in?
p and q
It can result in 4 different outcomes. The first can be true and the second can be true, the first can be true and the second can be false, and vice versa. You can express this in letters: TT, TF, FT, FF. Knowing this, the statement can only be two of those possible combinations, only two can be the result, TT or FT. The answer in this case, today, is true.
Now, how does this involve PHP? I'll write p and q in PHP form.
<?php $p = 'Tuesday'; $q = 2 + 7; ?>
I've only written Tuesday for p because we only need to check if today is Tuesday. The other words are just there so you speak proper English. Now let's set the variables for the actual results.
<?php
$today = date('l');
$addition = 2 + 7;
?>
I used the date function to get the current day's name (Monday, Tuesday, Wednesday etc). Now we'll compare the two using PHP's form of and. You can either write AND or &&. I prefer &&.
<?php
if ($p == $today && $q == $addition) {
echo 'Today is Tuesday and 2 plus 7 equals 9.';
} else {
echo 'Today isn\'t Tuesday or 2 plus 7 doesn't equals 9.';
}
?>
When we put everything together, we have this final bit of code.
<?php
$p = 'Tuesday';
$q = 2 + 7;
$today = date('l');
$addition = 2 + 7;
if ($p == $today && $q == $addition) {
echo 'Today is Tuesday and 2 plus 7 equals 9.';
} else {
echo 'Today isn\'t Tuesday or 2 plus 7 doesn't equals 9.';
}
?>
Result for today:
Today is Tuesday and 2 plus 7 equals 9.
That's your dose of PHP and math logic for today. 
I thought immediately about my overdue maths homework when I saw this. Bleh :P
hahaha
too bad you don't get extra credit =P
The second test for 9 should not be == it should infact be === because you want the datatype to match an int
'9' == 9 - will return (bool)true
9 === '9' - will return (bool)false
;)
Yay for logic! I love that stuff - I can't wait to take the Computer science course (at University - Waterloo) for it. Yumyum.
Mat is correct as well. If you don't see it, look at the single quotes. One is a char and one is a integer. For true equality: match datatype and value.
@Mat and Rebecca: They are both integers, I never made either one of them a char. However, once I finished writing this, I say my mistake. Being the lazy self I am, I didn't want to change it because I would have to reconvert all the code.
I should have used the triple equal sign, though.
Half the kids in my Geometry class don't get logic. It's really funny.