Loops and Conditionals

Loops and Conditionals

 

In this session we will cover:

  1. if blocks
  2. for loops
  3. nesting blocks

if

If is one of the most common English words. It expresses our drive and capacity to evaluate things, a trait critical to decision making. It’s no surprise that if is one of the most important and most commonly-used language features of PHP.

So how do we use if? Here’s a basic example:

1
2
3
4
$myAge = 40;
if ($myAge > 28) {
   echo 'Sorry, you are too old for this nightclub.';
}

The parentheses show PHP which bit of your code is the condition and the curly braces show PHP which code should be run if the condition is true. In this example the condition is $myAge > 28; in other words the statement on line 4 will only be echoed if $myAge is greater than 28.

Here’s how you use if to choose between two conditions:

1
2
3
4
5
6
7
8
9
10
11
$myAge = 40;
 
if ($myAge > 28) {
 
echo 'Sorry, you are too old for this nightclub.';
 
} else {
 
echo 'Welcome!';
 
}

…If you want to get more complex, you can add elseif conditions. Note that the final else will always and only be run if the other if and elseif conditions are false:

1
2
3
4
5
6
7
8
9
$myAge = 40;
 
if ($myAge > 28) {
   echo 'Sorry, you are too old for this nightclub.';
}  elseif($myAge < 21) {
   echo 'Sorry, you are too young for this nightclub.';
} else {
   echo 'Welcome!';
}

for

There are several ways of repeating code in PHP. For loops are on of the simplest. Let’s look at an example:

1
2
3
for ($i = 1; $i < 10; $i++) {
   echo $i . ', ';
}

This simply echoes:
1, 2, 3, 4, 5, 6, 7, 8, 9

to the browser. The for loop has 3 parameters: first a new variable called $i is set to an initial value of 1 with $i=1. Then for is instructed to keep looping while $i is less than 10. Then the for loop is told to add 1 to $i after each cycle of the loop. Each time through the loop it echoes the contents of $i and adds a comma and a space after for legibility.

nested blocks

These control structures are only useful in the context of other code. Here’s an example of how we can combine a for loop with a conditional:

1
2
3
4
5
6
7
8
for ($i = 1; $i < 16; $i++) {
 
   if (($i % 2) == 0) {
      echo $i . ' is even';
   } else {
     echo $i . ' is odd';
   }
}

…I don’t want to spend too much time on this, but you should pay attention to the way the if statement is nested inside the for statement. The use of curly braces and indentation makes this nesting readable. The curly braces are a feature of the PHP language; indentation is only for human readability.

This post is part of PHP First Steps Tutorial. Go on to the next session: Arrays.

24 hr tutorial Hey!
Based on the feedback from readers of this tutorial series I’ve expanded, revised, and improved this brief intro as a whole book. It is much longer, so you get a more in-depth guide to getting started. As a result you’ll write better code. When you buy the book you also get a download password for all the source code from the tutorials. Build a functioning blog from scratch, and other useful mini-projects! Check it out »

No Comments

Be the first to start the conversation.

Leave a Reply

*

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>