Php developers,Php Tutorials ,Web development, Web Developers, Php hosting, open-source,community
 
Serving the LAMP developers all over the word
LampDevelopers is a pure LAMP Developers community
Php developers,Php Tutorials ,Web development, Web Developers, Php hosting, open-source,community
LAMP Developers
   
   
   
   
   
   
Web Development
   
Developer / Programmer
   
Related Links
   
Member
   
Enter Keyword
Loops and Arrays

Loops and Arrays

This is the last parts of the tutorial we have showed you how to deal with text and variables in PHP script and how you can use IF statements to compare them and to make decisions. In this part we are going to show you how to use another important part of PHP, loops.

The WHILE Loop

The WHILE loop is one of the most useful commands in PHP scripting language. It is also quite easy to set up and used in PHP. 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 source code which you want to repeat several times without retyping it, you can use a while loop here. 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";
++$x;
}

We will now explain this source code. 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 source code has been executed. After these is the WHILE line. This tells the computer 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 that prints out the text, there is another very important line:

++$x;

What this does is exactly the same as writing:

$x = $x + 1;

It adds one to the value of $x. This source 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 move on to the next part of the code accepted.

Using $x

The variables use in this, 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) {
++$current;
echo "$current<br>";
}

There are few things to notice about this source code. Firstly, you will notice that we have placed the ++$current; before the echo statement run. This is because, if we 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 anthing 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

The arrays are the common things to use many programing languages. They are very special variables which can hold more than one value at a time, 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 system array is slightly different from setting up a normal variable. In this example we 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';

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

Reading From An Array

To reading something data from an array is just the same as putting some 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 we wanted to print out the third name we could use the code:

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

Which would output:

The third name is Steven

Using Arrays And Loops

One of the best use of a loop is to output its information in an array. For instance if we 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

We could use the following code:

$number = 5;
$x = 0;
while ($x < $number) {
$namenumber = $x + 1;
echo "Name $namenumber is $names[$x]<br>";
++$x;
}

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

News
this is the news
this is the news this is the news this is the news....
openWYSIWYG 1.01 bet....
small description
Links