Assuming you have read my basic introduction to PHP, you should have a fair understanding of what PHP is, how to write it, and how to assign and manipulate strings and numbers. In this tutorial, I will be teaching you about Arrays, Loops, and ‘if… else…’ statements. So, let’s get started.

Arrays

Say you had a big long list of related variables that you needed to store. For example, names of people. You could store them in separate strings, as you learned before, like this:

<?php
$name1 = "Matt";
$name2 = "Harry";
$name3 = "Bill";
$name4 = "John";
?>

However, doing this makes it difficult for them to interact with each other and makes looping through them all tricky. These problems can be solved by using an array. An array uses ‘keys’ to link the strings together in one variable. There are three types of array: numeric, associative, and multidimensional. I’ll leave out multidimensional arrays for now, and focus on numeric and associative as these are more common and easier to use. I will use a numeric array in this example, as there is only one factor linking the variables, the fact that they are all names.

There are two ways of assigning values to an array. The simplest way is to assign all the values in one line, like so:

<?php
$names = array("Matt", "Harry", "Bill", "John");
?>

A more complex way, but easier to understand when you look back at your code, is to assign the ‘keys’ yourself:

<?php
$names[0] = "Matt";
$names[1] = "Harry";
$names[2] = "Bill";
$names[3] = "John";
?>

Remember: Arrays start at 0, not 1!

In associative arrays, you assign your own keys, like this:

<?php
$ages = array("Matt"=>20, "Harry"=>30, "Bill"=>35, "John"=>55);
?>

Or like this:

<?php
$ages['Matt'] = 20;
$ages['Harry'] = 30;
$ages['Bill'] = 35;
$ages['John'] = 55;
?>

Easy! And it’s just as easy to access your arrays, whether it’s numeric or associative, you access the value using the array name and the key. For example:

<?php
echo $names[2]; //Will echo 'Bill'
echo $ages['John']; //Will echo '55'

echo $ages[$names[1]]; //Will echo '20' (Although this isn't good practice...)
?>

But what if you want to solve the original problem of looping through the array… that’s where loops come in!

Loops

Before we move onto looping through arrays, I’ll teach you some of the more basic loops in PHP. The ‘while’ loop is the most basic loop. It takes one condition and repeats a block of code for as long as that condition is true. You can use this in many ways. For example, you could write out a times table:

<?php
$table = 5;
$x = 1;
while($x<=12)
   {
   $result = $table*$x;
   echo $result . "<br />";
   $x++;
   }
?>

This code would produce:

5
10
15
20
25
30
35
40
45
50
55
60

Let me explain, we first set the value of $table to the times table we want to write out, in this case ’5′. We then initialise $x as ’1′, to make sure we start the times table from 1. Then, the loop; we say while $x is less than or equal to 12, do this code. This means that we will start the 5 times table at 1 and end at 12. Inside the loop, we put the code which produces the output. We multiply $x by the $table then echo it, along with a line break so it’s clearer. Finally, we increment $x so it is one more than last time to continue the table. When $x reaches 13, the loop stops. Easy, right?

Well, there’s an easier way! The ‘for’ loop is harder to write but can save a few precious lines of code. The same output as before can be produced from this code:

<?php
$table = 5;
for($x = 1; $x<=12; $x++)
   {
   $result = $table*$x;
   echo $result;
   }
?>

In this loop, the initialising and the incrementing is put in when we write the loop condition. This clears up the code inside the loop and enables it to be more easily understood. However, in some cases, ‘while’ loops are better than ‘for’ loops, so keep that in mind.

The final loop is the ‘foreach’ loop, as the name implies, this loop is used to loop through all the values in an array. We’ll use our $ages array from earlier along with the ‘foreach’ loop to create a table of people and their ages.

<?php
echo "<table>";
echo "<th><td>Name</td><td>Age</td></th>";
foreach($ages as $name=>$age)
   {
   echo "<tr><td>" . $name . "</td><td>" . $age . "</td></tr>";
   }
echo "</table>";
?>

We start the loop by writing $ages as $name=>$age, this means that PHP takes the values one at a time, puts the key in $name and the value in $age, then starts the loop. This happens with each value until there are no more, then the loop stops.

So… that’s it, you know how to assign and access variables in arrays, loop through simple values using ‘while’ and ‘for’, and how to loop through arrays using ‘foreach’. Let’s move on…

If… Else…

The ‘if… else…’ statement is used to execute different blocks of code depending on whether a condition is true. This could be used to check somebody’s answers from a quiz question, and display a message depending on whether they got it correct or not.

<?php
$answer = $_POST['answer'];
if($answer == "football")
   {
   echo "<p>You are correct!</p>";
   }
else
   {
   echo "<p>Incorrect, try again!</p>";
   }
?>

It is a very simple statement to use, and can often be used without the ‘else…’ if you are displaying something only if a condition is true. However, it can be made more complex with the ‘elseif…’ part, allowing another condition to be checked if the first is false. For example:

<?php
$answer = $_POST['answer'];
if($answer == "football")
   {
   echo "<p>You are correct!</p>";
   }
elseif($answer = "rugby")
   {
   echo "<p>Almost right... think harder!</p>";
   }
else
   {
   echo "<p>Totally wrong, try again!!</p>";
   }
?>

Now, with your knowledge of variables, operators, arrays, loops, and statements, you should be capable of producing a simple web application. Why not give it a go while you’re waiting for the next edition, where you will learn about functions, how to use them, and how to write your own. Make sure you don’t miss it by subscribing to my RSS feed.

Also, if you missed last weeks article, take a look. You never know, you might forget something basic one day!