In the last part of this series, we learned about using variables in PHP. Today, we will learn about using program control structures, such as if/else blocks and for/while loops. There are a number of other control structures, but these are the most common, and can accomplish everything that the other ones can.
Conditional Execution Using an If/Else Block
There are many circumstances where we would want to execute a certain block of code only if some other condition were true. In times like this, we want to make use of an if/else block. Let’s see how we could use this method to display one message for all visitors that came from digg, and a different message for everyone else:
<?php
if (stristr($_SERVER["HTTP_REFERER"], "http://digg.com") === TRUE) {
echo "Greetings digg users!";
}
else {
echo "Welcome to my website!";
}
?>
Let’s take a closer look at exactly what is going on here. $_SERVER[“HTTP_REFERER”] is a built-in server variable that contains the referring website, if there is one. The function stristr searches for one string within another string. In this case, we are searching for “http://digg.com” within the referrer contained in $_SERVER[“HTTP_REFERER”]. If “http://digg.com” is found within the referrer string, then stristr will return TRUE, so this is the condition that we check for in our if statement. The code that we want to execute when this occurs follows the if statement, contained within curly braces {}. If the statement evaluates to FALSE, then the code that follows our else statement will be executed (also contained within curly braces). If we had not wanted to display a default message, we could have simply left off the else statement altogether.
If we had multiple conditions to test, we could have used the elseif statement. Let’s look at a simple example of how this could be used:
<?php
$gradePercent = 75;
if ($gradePercent > 90) {
echo "You got an A!";
}
elseif ($gradePercent > 80) {
echo "You got a B!";
}
elseif ($gradePercent > 70) {
echo "You got a C!";
}
elseif ($gradePercent > 60) {
echo "You got a D!";
}
else {
echo "You got an F!";
}
?>
Using For/While Loops
On some occasions, you will need to loop through a list of values – to list your 5 most recent blog posts, for example. You have several different choices of loops to use, depending on your exact situation. The 2 most popular loop structures that I will cover are for and while loops. The while loop is a bit simpler, so I will cover that one first:
<?php
$count = 1;
while ($count <= 10) {
echo "$count ";
$count++;
}
?>
The conditional statement in the while loop says that the loop will be executed as long as $count is less than or equal to 10. Inside of the loop, we just print out the current value of our variable $count, and then increment $count by 1 ($count++ is a shorthand statement that is equivalent to $count = $count + 1). Note that all the steps we want to perform inside of our loop are contained within curly braces, just as we did with the if/else statements above. The result of this loop would be to print the numbers 1 through 10 on the screen.
Now let’s take a look at how we would do the same thing with a for loop:
<?php
for ($count = 1; $count <= 10; $count++) {
echo "$count ";
}
?>
You will notice that the for statement is a bit more complicated than the while loop, but it also does a lot more for us. There are 3 parts that must be included in a for statement, separated by semi-colons: the first initializes our loop counter, the 2nd is the exit condition, and the 3rd increments the loop counter after each pass through the loop. One reason I like the for loop is that it forces me to remember to increment the loop counter, whereas the while loop does not. If you forget to increment the loop counter, the exit condition will never be satisfied, resulting in an infinite loop!
For Next Time
Now that you have learned about using if/else blocks and for/while loops, you can really start to write some interesting programs in PHP. For the next part in this series, I would love to get some feedback on what you’d like to hear about next. Do you want to learn about creating your own functions, or how about database access? I’m wide open to suggestions, so leave ’em here in the comments, and tell me what you’d like to learn about next.
You can already read Getting Started with PHP: Part 4 – Integrating PHP and HTML.