Site icon Code Exercise

Best PHP Tutorial For Beginners (while & for Loops) Part- 4

In the last part -4 of this tutorial I have showed you how you can use IF statements to make decisions and to compare them and how to deal with variables and text in PHP. In this part I am going to show you how to use another important part of PHP, Loops.

Following looping statements in PHP:

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

The While Loops statement

Syntax:

<?php
//loops through a block of code if and as long as a specified condition is true.
while (condition is true) {
    code to be executed;
}
?>

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 earth” 10 times you could use the following code:

$ten_times = 10;
$x = 0;
while ($x < $ten_times) {
echo "Hello earth";
++$x;
}

Now I will explain this code. The first 2 lines are just putting the variables. The $ten_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 computer to repeat the code while $i is less than $ten_times (or to repeat it until $i is equal to $ten_times). This is followed by the code to be executed which is enclosed in { }.

After the echo line which prints/echo (results) 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 code is then repeated (as $x now equals 1).

Do…While loop statement in PHP:
The do…while statement will execute a block of code at least once – it then will repeat the loop as long as a condition is true.

Do while Loops statement

Syntax:

do {
 code to be executed;
}
while (condition);

Example: The following example will increment the value of i at least once, and it will continue incrementing the variable i as long as it has a value of less than 10 −

 <?php
 $i = 0;
 $num = 0;
 do {
 $i++;
 }
 
 while( $i < 10 );
 echo ("Loop stopped at i = $i" ); //Loop stopped at i = 10
 ?>

For Loop Statement in PHP:
The for statement is used when you know how many times you want to execute a statement or a block of statements.

The For Loop Statement

Syntax:

for (initialization; condition; increment){
 code to be executed;
}

The initialize is used to set the start value for the counter of the number of loop iterations. A variable may be declared here for this purpose and it is traditional to name it $i .

Example: The following example makes five iterations and changes the assigned value of two variables on each pass of the loop −

 <?php
 $a = 0;
 $b = 0;
 for( $i = 0; $i<5; $i++ ) {
 $a += 10;
 $b += 5;
 }
 echo ("At the end of the loop a = $a and b = $b" ); // At the end of the loop a = 50 and b = 25
 ?>

Foreach Loop Statement in PHP:
The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

Foreach Loop Statement

Syntax:

foreach ($array as $value) {
 code to be executed;
}

For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.

The following example demonstrates a loop that will output the values of the given array ($colors):

Example Foreach loops Code:

<?php 
$colors = array("1", "2", "3", "4");

foreach ($colors as $value) {
 echo "foreach Result is $value 
";
}
?>

This will produce the following result −

 foreach Result is 1
 foreach Result is 2
 foreach Result is 3
 foreach Result is 4
 foreach Result is 5

Next Part 5

In the next part I will show you how you learn Arrays.

Exit mobile version