Tutorial Materials - Free Detailed Photoshop & Web Development Tutorials.
Understanding basic PHP IF/Else statements
Posted in Basics

phpifelse.gif

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’.

Then the IF starts, it’s basically saying this: If the variable $answer equals exactly to right then display this, if it’s wrong.. then display this.Ehco means display, so if it’s right we want to echo ‘Correct, Tutorial Materials does rock!‘, then the ‘else‘ kicks in and this also echo’s something if the answer is not equal to right.

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.

Posted by PicoDeath on Dec 19, 2007

You have a mistake. Should be you’re, and not your. =)

Comment by brian — January 1, 2008 @ 8:32 am

» | RSS feed for comments on this post. TrackBack URI

Leave a comment







Navigation
  • Home
  • About
  • Hosting
  • Advertise
  • Contact
Photoshop Tutorials
  • Animation (0)
  • Basics (0)
  • Designing (1)
  • Photo Effects (1)
  • Photo Retouch (0)
  • Text Effects (1)
  • Web Graphics (2)
  • Web Layouts (0)
Web Design Tutorials
  • Basics (3)
  • Menus (0)
  • Tricks/Tips (0)
  • Validation (0)
PHP Tutorials
  • Basics (1)
  • Cookies/Sessions (0)
  • Forms (0)
  • Security (0)
Affiliates
  • Web Analytics
  • Fresh-Myspace
  • 2sided.net
  • Photoshop Tutorials
  • Go Media Zine
Sponsors
Designed & Coded by Leon Calder