Vectorials
Flash Perfection
3D Lessons
Tutorialkit
Markup Tutorials
Learn PHP
network
adv banner
Web Programming  Home Web Programming PHP If/Else Statements
rss

If/Else Statements

Author: GreyCobra.com More by this author


If/Else Statements1. If and Else statements are special pieces of code that tell your server to do something under a certain condition.  If and Else statements therefore are conditional statements.  An example of a likely piece of code that uses these statements is:

If x=true, then do this

Of course we can customize this to fit our needs.

In this tutorial, we will use the conditionality statements to display a message based on a statement.  Create a new document called ifelse.php, and insert the following code:

<?php //Start php
$number = 10;//Set the variable number to equal 10

if ($number<=15)//if the variable $number is less than or equal to 15...
{//Than do this
echo "The number is less than 15";//Shows the message for being less than or equal to 15
} else {//However if the variable $number is not less than or equal to 15, then do this...
echo "The number is greater than 15";//Shows the message for being greater than 15
}//end the if else statement

?>

2. What is this code going to do?  When executed on the server, the page displayed will depend on the value of the variable you defined as $number.  If $number is less than 15, then it will display one message, but if it is greater than 15, it will display another message.  As you can see, we used a simple if/else statement.  Lets look at how we set it up:

if (condition)
This is what sets up our if statement

{
Do this
}
If the condition is true, do whatever is in this function.  Anything inside of the curly braces is included in the function.

Else
This tells it that if the conditions in the If statement are not met, then do the following function

{
Do this
}
Tells the code to do in case the condition is not met

3. Alright, now we have a code set up with one condition.  However, lets say we want our code to test for multiple conditions.  For example, lets use colors:
IE: If $color = green, then display green, but if its red, display red, and if its blue, display blue, etc...
To do this, we can use the following code:

<?php //Start php

$color = blue; //Define the variable $color

if ($color = red) //If the color is red...
{//do this
echo "The color is red";
} else if ($color = green) {//however if the color is green, do this
echo "The color is green";
} else if ($color = blue) {//And if the color is blue, do this
echo "The color is blue";
} else {//And finally, if color matches none of the conditions, display this
echo "The color is not red, green, or blue";
}//end the statements
?>

4. We see a new piece of code in the above statement:

else if

If the first "if (Condition)" is not true, see if it true with "Else if (condition). There can be many if else statements in your code, making them very useful in the language.

Well, That's basically it.  Obviously, you probably won't need a code for colors on your site, but if you use your new knowledge, you can probably put this to good use.  Good Luck!



Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Add comments to "If/Else Statements"