Tuesday, November 25, 2008

Lesson 5 : If/Else Statements

Objective
This lesson will teach you how to use If / Else Statements to choose between two
options and to process data in different ways. Think of an If / Else Statement the
way you would think of a fork in the road. You come to the fork and you must
choose to go left or go right. When your program comes to an If / Else Statement,
the program will choose to go way or another.
Step 1
Open a new window in your Text Editing Program.
Step 2
Type in the lines just as they appear in the text below:


$x = 1;
if ($x == 1) {
echo “This is the If Statement “;
}
?>
Step 3
Name this file if.php and save it in your Project Folder.

Step 4
Upload if.php to your server.

Step 5
View the page (www.YourWebDomain.com/if.php) in your Web Browser. You
should see the words “This is the If Loop” in your Web Browser.

We just created a file that creates a variable $x and then uses an If Statement to
compare $x to the value of 1. Two equal signs are read as ‘is equal to’, so the
statement if ($x == 1) is read as ‘if $x is equal to 1’.
In this case, $x is equal to 1, so the program executes the code that is contained
within the brackets.
Line 2

$x = 1;
This line creates the variable $x and gives it a value of 1.

Line 3

If ($x == 1) {
This line begins the If Statement. The code between the parenthesis is the Test.
If the Test is true, then the code between the brackets will be executed.

Line 4

echo “This is the If Statement “;
This is the code that appears between the brackets. Since the Test (line 3) is
true, the code between the brackets is executed and the message ‘This is the If
Statement” is printed to the screen.

Line 5

}
This line ends the If Statement. All If Statements must have a beginning and
ending bracket.


Exercise 5-1
You can place an Else Statement after an If Statement. If the If Statement is not
true, the Else Statement will be executed instead. Here we will modify if.php to
contain an If / Else Statement.


$x = 1;
if ($x == 1) {
echo ‘This is the If Statement ’;
}
else {
echo ‘This is the Else Statement ‘;
}
?>


Try changing the value of $x to create a different result.


Exercise 5-2
Aside from ==, you can use many different tests within an If Statement. Some of
these tests are listed in the table below.


Test Meaning
$x > 1 If $x is greater than 1
$x < 1 If $x is less than 1
$x >= 1 If $x is greater than or equal to 1
$x != 1 If $x is not equal to 1


You also have the ability to use multiple If / Else Statements in succession, to
narrow down your choices. In this exercise we will use multiple If / Else
Statements.



$x = 1;
if ($x == 1) {
echo ‘X is equal to 1 ’;
}
else if ($x > 1) {
echo ‘X is greater than 1 ‘;
}
else if ($x < 1) {
echo ‘X is less than 1 ‘;
}
else {
echo ‘X is not a number ‘;
}
?>


Try changing the value of $x to create different results.


Conclusion
In Lesson 5 we learned how to use If / Else Statements to run different
operations, based on the value of a variable. If / Else Statements can be very
useful when determining what data to display on your screen, or what functions
to run. In the future we will collect information from users via a Form (Lesson 4)
and then use If / Else Statements to help process that data.
In Lesson 6 we will explore how to use While Loops to count and process data.

No comments: