PHP for Beginners: Part 2 – Making Decisions and Loops

Over the past part I have shown you the basics of text in PHP and how to store it as variables. In this part of the tutorial I will show you firstly how to use if statements to make decisions in your scripts. Next, I am going to show you how to use another important part of PHP, loops.

Link to Part 1


The Basics Of if

If statements are used to compare two values and carry out different actions based on the results of the test. If statements take the form IF, THEN, ELSE. Basically the IF part checks for a condition. If it is true, the then statement is executed. If not, the else statement is executed.

if Structure

The structure of an if statement is as follows:

if (something == something else) {
THEN statements
} else {
ELSE statements


The most common use of an if statement is to compare a variable to another piece of text, a number, or another variable. For example:

if ($username == "webmaster")

which would compare the contents of the variable to the text string. The THEN section of code will only be executed if the variable is exactly the same as the contents of the quotation marks so if the variable contained ‘Webmaster’ or ‘WEBMASTER’ it will be false.

Constructing The THEN Statement

To add to your script, you can now add a THEN statement:

if ($username == "webmaster") {
echo "Please enter your password below";

This will only display this text if the username is webmaster. If not, nothing will be displayed. You can actually leave an IF statement like this, as there is no actual requirement to have an ELSE part. This is especially useful if you are using multiple IF statements.

Constructing The ELSE Statement

Adding The ELSE statement is as easy as the THEN statement. Just add some extra code:

if ($username == "webmaster") {
echo "Please enter your password below";
} else {
echo "We are sorry but you are not a recognized user";

Of course, you are not limited to just one line of code. You can add any PHP commands in between the curly brackets. You can even include other IF statements (nested statements).

Other Comparisons

There are other ways you can use your IF statement to compare values. Firstly, you can compare two different variables to see if their values match e.g.

if ($enteredpass == $password)

You can also use the standard comparison operators to check to see if one variable is greater than or less than another:

if ($age < "13") 


if ($date > $finished)

You can also check for multiple tests in one IF statement. For instance, if you have a form and you want to check if any of the fields were left blank you could use the following code:

// || - OR logical operator
if ($name == "" || $email == "" || $password == "") {
echo "Please fill in all the fields!";

Now it’s time to show you some other ways of using your PHP script to do other types of checks and loops. So, let’s start.

The while Loop

The while loop is one of the most useful commands in PHP. It is also quite easy to set up and use. A while loop will, as the name suggests, execute a piece of code until a certain condition is met.

Repeating A Set Number Of Times

If you have a piece of code which you want to repeat several times without retyping it, you can use a while loop. For instance if you wanted to print out the words “Hello World” 5 times you could use the following code:

$times = 5;
$x = 0;
while ($x < $times) {
echo "Hello World";

The first two lines are just setting the variables. The $times variable holds the number of times you want to repeat the code. The $x variable is the one which will count the number of times the code has been executed. After these is the while line. This tells the php interpreter to repeat the code while $i is less than $times (or to repeat it until $i is equal to $times). This is followed by the code to be executed which is enclosed in { }.

After the echo line which prints out the text, there is another very important line:


What this does is exactly the same as writing:

$x = $x + 1;

It adds one to the value of $x. This code is then repeated (as $x now equals 1). It continues being repeated until $x equals 5 (the value of times) when the computer will then move on to the next part of the code.

Using $x

The variable counting the number of repeats ($x in the above example) can be used for much more than just counting. For example if you wanted to create a web page with all the numbers from 1 to 1000 on it, you could either type out every single one or you could use the following code:

$number = 1000;
$current = 0;
while ($current < $number) {
echo "$current";

There are a few things to notice about this code. Firstly, you will notice that I have placed the ++$current; before the echo statement. This is because, if I didn’t do this it would start printing numbers from 0, which is not what we want. The ++$current; line can be placed anywhere in your WHILE loop, it does not matter. It can, of course, add, subtract, multiply, divide or do anything else to the number as well.

The other reason for this is that, if the ++$current; line was after the echo line, the loop would also stop when the number showed 999 because it would check $current which would equal 1000 (set in the last loop) and would stop, even though 1000 had not yet been printed.


Arrays are common to many programing languages. They are special variables which can hold more than one value, each stored in its own numbered ‘space’ in the array. Arrays are extremely useful, especially when using while loops.

Setting Up An Array

Setting up an array is slightly different to setting up a normal variable. In this example I will set up an array with 5 names in it:

$names[0] = 'John';
$names[1] = 'Paul';
$names[2] = 'Steven';
$names[3] = 'George';
$names[4] = 'David';

or just:

$names[] = 'John';
$names[] = 'Paul';
$names[] = 'Steven';
$names[] = 'George';
$names[] = 'David';

or simply:

$names = array('John', 'Paul', 'Steven', 'George', 'David');

As you can see, the parts of an array are all numbered, starting from 0. To add a value to an array you must specify the location in the array by putting a number in [ ].

Reading From An Array

Reading from an array is just the same as putting information in. All you have to do is to refer to the array and the number of the piece of data in the array. So if I wanted to print out the third name I could use the code:

  echo "The third name is $names[2]"; // Steven

Which would output: The third name is Steven

Using Arrays And Loops

One of the best uses of a loop is to output the information in an array. For instance if you wanted to print out the following list of names:
Name 1 is John
Name 2 is Paul
Name 3 is Steven
Name 4 is George
Name 5 is David

You could use the following code:

$x = 0;
while ($x < sizeof($names)) {
$namenumber = $x + 1;
echo "Name $namenumber is $names[$x]";

As you can see, you can use the variable $x from the loop to print out the names in the array. You may have noticed I am also using the variable $namenumber which is always greater than $x. This is because the array numbering starts from 0, so to number the names correctly in the final output I must add one to the actual value.


In this part of the tutorial I have showed you how you can use if statements to compare variables and text information and to make decisions. We’ve also looked at using another important part of PHP, loops. In the next part I will show you how you can send e-mails using PHP. Stay tuned.

Nick Plekhanov

Hi there! My name is Nick Plekhanov. I'm a passionate web developer and user interface designer from Russia, experienced in PHP, and MySQL along with the beautiful XHTML and CSS under jQuery stuff. I've been doing web development for 3 years. Here, I'll be your PHP coder, sometimes designer, and SEO expert. My own web development blog is coming really soon. If you’d like to connect with me, follow me on Twitter.

15 Smart Tools To Help You Build Your Freelance Business

Discover the awesome tools we use in making our clients comfortable and happy in learning new things every day.

Download Now


  1. Rahul says

    yes i remember the 1st part and i did tell you my knowledge for this languages is getting better , i’m trying to improve and reading these posts helps a lot , your post was pretty clear to me and i will be focusing more on this ! thank you for sharing this :) waiting for more

  2. Anonymous says

    Bad practice to have function calls in loop declarations.while($x<sizeof(array))should be$listCount=sizeof(array) while($x<$listCount)If you have it in the declaration (while(func), for(func)) the function will get called on each iteration, which means you could get a very very slow loop if the function takes a long time to run.

  3. says

    Really a good post including all loop examples. while loops are the simplest type of loop in PHP because they behave just like their C counterparts. Using your example I have solved my so many problems so thanks for sharing.

  4. Javad Absalan says

    I was waiting for this part after reading part 1. Really nice and comprehensive guidance for beginners. Good Luck