PHP - Making PHP loop in circles

From Global Programming Syntax

Jump to: navigation, search

In this tutorial you will learn the loopedy loop loop syntax as I call it. Basically it makes php repeat some code with a few variables changed each time making it redo simular things. At the same time if you make an infinit loop it is possible to crash the server.

While loops

Below is an example of the basic while loop displaying numbers 1 to 9.

$id=1;
while ($id<10) {
echo $id;
echo '<br>';
$id+=1;
}

Now if your an expert you may say a for loop will be by far better and a little faster but for now lets start at the basics. In the above script, just before the loop, the variable is set to 1. Then comes a loop. The line with while($id<10 { basically tells the computer while id is smaller than ten, continue to loop. Then between the brackets is what is done during each repeat of the loop. In this case, the loop is repeated nine times and each time adds one to id and displays id.

Also in addition to the above syntax, you may force php to do the loop at least once and then check weather to continue instead of checking at the very beginning. To do this you use what's called a do-while loop and an example is as follows:

$id = 1;
do {
echo $id;
echo '<br>';
} while ($id==10);

Now with the above code, even though the while condition doesn't match, it will still process the first round. As useless it may seem to a noob it can save duplicating code in rare situations.


In the next example you will learn how to use loops to loop through the rows of a mysql database. Below is the basic code is simular to the previous example.

$result=mysql_query('SELECT * FROM `table`');
while ($row=mysql_fetch_assoc($result)) {
echo $row['column_name'];
echo '<br>';
}

So the above is the basic syntax for looping through a mysql database. It shows on the first line the mysql result being assigned to a variable. Then in the while statement $row equals the value of the row but if however mysql_fetch_assoc returns false (no row) then the loop will end. And during each loop a column is displayed on a new line.

For loops

For loops are basically like an advanced version of the while loop. The basic structure of a for loop is devided into three parts between the brackets. The first is what to do before the loop begins. In the example below it sets the id to one. Then in the middle is the statement (same as the while loop). Then at the right hand side is what to do before every loop round unlike the first part which only occurs once.

for ($id=1 ; $id<10 ; $id+=1) {
echo $id;
echo '<br>';
}

However sometimes the above code will not allways be written in that format. Below is the same thing just different variable names and a different way of adding +1.

for ($i=1;$i<10;$i++) {
echo $i.'<br>';
}

The above code may confuse some people but if you compare the for loops above, they both do the same thing but the second method is more efficient.

Foreach loops

Foreach loops are generally used to loop through arrays to retrieve sub arrays or keys and values. The array key is basically what goes between the [] brackets when using an array. Below is an example of using a foreach loop to count to nine.

$numbers=array(1,2,3,4,5,6,7,8,9);
foreach ($numbers AS $value) {
echo $value.'<br>';
}

But the above example only shows how to retrieve the values. What about the keys. Well if for example you wanted to count both letters and numbers using keys and values then the following is an example of how to do that.

$array=array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6,'g'=>7,'h'=>8,'i'=>9);
foreach ($array AS $key=>$value) {
echo '$array[\''. $key ."']=". $value .'<br>';
}

So the above code will display the array in a readable format. Give the above examples a try yourself and see what they do.

Personal tools
languages
page stats
Toolbox