PhpCookies About Cookies
logo
Subscribe (full feed)
Enter your email address:
Delivered by FeedBurner
rss
» November 25, 2007 in   posted by koen
I was talking with someone on MSN, who explained me that she had problems with if-statements. That it was like learning maths. I don't have much problems with it, but others may have.

So today, let's talk about the basics of if-statements.

What IS it?
They are the possibility to check variables (that's just one thing) against another variable. Or value against variable (or otherwise).

An example can be a simple dice. I first use the rand() function to produce a random number between 1 and 6. Then, if the dice returns 5, I output something. Basically, this is the #5 Dice game (doesn't exists, I hope?). When the dice returns 5, the player wins, else the player loses.

<?php
$dice = rand(1,6);
if($dice == 5)
{
echo "You won!";
}else{
echo "You loose!";
}
?>


Fairly simple... right? Note that if you use $dice = 5, you actually assign the number 5 to the variable $dice, which will usually always return to true, meaning that it looks like the dice always returns 5 in this case!

What's more?
What if you have the same dice, but now if the dice is 4,5,6 (basically, any number higher than 3), the player wins, else looses. This can be done in two different ways. Either check whether the number is higher than 3, or whether the number is 4 or higher. When doing higher than 3:

if($dice > 3)


When doing 4 or higher:

if($dice >= 4)


This isn't acracadabra, right!?
Let's move on! The type comparing operator, === and !==. This will check if the types of the two values match, in addition to the value. For example:

<?php
if((int) 5 == "5")
{
echo "Exactly the same!";
}
if((int) 5 === "5")
{
echo "Exactly the same!";
}
?>


You would guess that 5 is still 5 right? But the second if-statements says, hey, the first one is an integer, the other one is a string!

Now, on to the next example: the dice must NOT return 5, or the player will loose.

if($dice != 5)
{
echo "You won!";
}


And another example, the dice must be either 1 or 6, in order for the player to win.
if($dice == 1 || $dice == 6)
{
echo "You won";
}


As you can see, || means OR. If you want to do AND, you do &&. With the OR statements, both sides of the || can be true, with the AND statement, both must be. We also have the exclusive or, or XOR. Then either one must be true, but NOT both. For example, we have two dices. One of them must be the number 1, but NOT BOTH!

<?php
$dice_one = rand(1,6);
$dice_two = rand(1,6);
if($dice_one == 1 ^ $dice_two == 1)
{
echo "You won!";
}
?>


I think that's enough brain-cracking for today.


Good luck using this ;)



Mac (33) says on November 25, 2007:
Thank God there are people like you Koen, that make php look like kids play. To me, php has always been unattainable and hard to wrap my head around to. One day maybe with the help of your site, I'll be able to say "wow that's easy!"

Even though I don't understand everything you write about on this site, I read and try and research on my own.

Take care fellow Apple fan ;)
Mieke says on November 26, 2007

Hi Koen

You're
It was only afew hours ago I told you about my problems with the 'if' statement, and here you are...

And, believe it or not, for once it doesn't look like maths at all. When you ask people for help with PHP, most of them forget to 'translate' into normal English. They forget that the other one doesn't have the knowledge they have, so they rush over things. But not you... :o)

Although I only discovered your site a few days ago, I already know I've finally found what I've been looking for for so long - PHP in plain English

Keep up the good work!

Greetz!
Mieke
Lord Matt says on November 26, 2007

I love ifs. When doing my IT education for the paper work that I now ignore that tells the world what I could do I had to "do" spreadsheets. To make it more fun I found add complex bells and whistles to it out of heavily nested instant if (IIF). There was a gang of us and I would do the "logic" for us all. I still get a kick out of data logic which is why I'm a geek.