Understanding IF’s is important in PHP for many reasons. They’re mainly used when submitting a HTML form, you can see them being uses in nearly every contact script. When you accidentally submit the wrong text you’ll get an error back, this is using PHP IF’s.
However, they’re also used for tons of other stuff but you’ll figure that out later, so let’s make you understand!
$answer = right;
if ( $answer == right ) {
echo “Correct, Tutorial Materials does rock!”;
} else {
echo “You’re wrong, Tutorial Materials does not suck”;
}
Here’s what a fully completed IF + Else if statement looks. Here’s how it works, anything with a $ in front it means it’s a variable, which means it stores information. We stored ‘right’ in the variable called ‘$answer’.
Correct, Tutorial Materials does rock!
Because ‘$answer’ equals to right, the correct message has been display(and is true, we do rock).
$answer = right;
if ( $answer == wrong ) {
echo “Correct, Tutorial Materials does rock!”;
} else {
echo “You’re wrong, Tutorial Materials does not suck”;
}
On this peice of code, I’ve changed the IF. It’s the same code but this time $answer does not equal to right, therefor it’s wrong and will display the else if.
You’re wrong, Tutorial Materials does not suck
This is the error message returned, which is false.
That’s it! The basics of IF’s and Else If’s.
You have a mistake. Should be you’re, and not your. =)
Comment by brian — January 1, 2008 @ 8:32 am