For Loop Java

For Loop in PHP

PHP FOR LOOP

The for loop in php is the most complex loop in PHP that is used when the user knows how many times the block needs to be executed. The for loop contains the initialization expression, test condition, and update expression (expression for increment or decrement).

SYNTAX –

for (init counter; test counter; increment counter) {
  code to be executed for each iteration;
}

EXAMPLE

<?php

for ($i = 0; $i < 10; $i++){

$product = 10 * $i;

echo "The product of 10 * $i is $product \n";
}

?>


OUTPUT

The product of 10 x 0 is 0 

The product of 10 x 1 is 10 

The product of 10 x 2 is 20 

The product of 10 x 3 is 30 

The product of 10 x 4 is 40 

The product of 10 x 5 is 50 

The product of 10 x 6 is 60 

The product of 10 x 7 is 70 

The product of 10 x 8 is 80 

The product of 10 x 9 is 90

PHP NESTED FOR LOOP

We can use for loop inside for loops in PHP, it is known as nested for loop. The inner for loop executes only when the outer for loop condition is found true.

In case of inner or nested for loop, nested for loop is executed fully for one outer for loop. If outer for loop is to be executed for 3 times and inner for loop for 3 times, inner for loop will be executed 9 times (3 times for 1st outer loop, 3 times for 2nd outer loop and 3 times for 3rd outer loop)

CODE –

<?php    
for($i=1;$i<=3;$i++){    
for($j=1;$j<=3;$j++){    
echo "$i   $j<br/>";    
}    
}    
?>


OUTPUT –

1 1

1 2

1 3

2 1

2 2

2 3

3 1

3 2

3 3

The foreach loop statement

The foreach statement is used to loop through arrays. For each pass, the value of the current array element is assigned to $value and the array pointer is moved by one and in the next pass next element will be processed.

Syntax

foreach (array as value) 

{

   //code;

}

EXAMPLE – 

<?php

         $array = array( 1, 2, 3, 4, 5);

         foreach( $array as $value ) {

            echo "Value is $value <br />";

         }

?>


OUTPUT  –

Value is 1

Value is 2

Value is 3

Value is 4

Value is 5

THE CONTINUE STATEMENT

The PHP continue keyword is used to halt the current iteration of a loop but it does not terminate the loop.

Just like the break statement the continue statement is situated inside the statement block containing the code that the loop executes, preceded by a conditional test. For the pass encountering continue statement, rest of the loop code is skipped and next pass starts

EXAMPLE –

In the following example loop prints the value of array but for which condition becomes true it just skip the code and next value is printed

<?php

$array = array(1, 2, 3, 4, 5);

foreach ($array as $value) {
    if ($value == 3) {
        continue;
    }
    echo "Value: $value \n";
}

?>


OUTPUT –

Value is 1

Value is 2

Value is 4

Value is 5
  1. init counter: Initialize the loop counter value
  2. test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
  3. increment counter: Increases the loop counter value

Looping is a very useful and important part of every programming language. In this tutorial, we will learn full functionality and working of php for loop.
Loops are used to perform a set of statements continuously until a particular condition is satisfied. We have the following types of loops.

1.) For Loop
2.) While Loop
3.) Do-While Loop

For Loop PHP
For Loop Java

1.) For Loop Syntax in PHP

for(initialization; test condition; increment or decrement){
Statement or Statements;
}

In the above for loop syntax, we have used following three terms.

a) Initialization : It means to initialize some value to variable.
Example: $i=1; i.e β€œi” is just variable we can take any one.

b.) Test Condition : It will test the condition upto for loop will execute.
Example: $i < 10;

c.) Increment or Decrement : Here we will increase or decrease given variable (in our case: i).
Example: $i++ or  $i – – ;

Example of for loop using above syntax:

<?php
for($i=1; $i < 10; $i++)
{
  echo("<br>Value of i Variable is: ". $i);
}
?>


Output of above Example is:

Value of i Variable is: 1
Value of i Variable is: 2
Value of i Variable is: 3
Value of i Variable is: 4
Value of i Variable is: 5
Value of i Variable is: 6
Value of i Variable is: 7
Value of i Variable is: 8
Value of i Variable is: 9

2.) Infinite loop in PHP

Sometimes according to requirement, we need to use Loop that will execute infinite times. In infinite loop, we write a condition that never return false.

Infinite For Loop Syntax in PHP

for(;;){
//Statement that will execute infinite times
}

Example:

<?php
for( ; ; )
{
  echo(" This text will be print infinite times ");
}
?>


Output :

This text will be print infinite times
This text will be print infinite times
This text will be print infinite times
This text will be print infinite times….

Note: Above text will print infinite times.

3.) Iterate Array using For Loop

An array is a collection of data with the same datatype. We have two ways to Iterate Array using for Loop.
a) Simple for loop

<?php
$a = array("for loop php","foreach loop","php example programs");
for($i=0; $i<count($a); $i++)
{
    echo "<br>".$a[$i];
}
?>


b) PHP foreach

<?php
$arr = array("for loop php","foreach loop","php example programs");
foreach($arr as $ar)
{
	echo "<br>".$ar;
}
?>


Note: The output of above both programs will be the same

for loop php
foreach loop
php example programs

4.) Break for loop using the break keyword

break keyword is a very useful keyword in PHP. We use the break keyword to stop the iteration of any loop before completing.

<?php
for($i=1; $i < 10; $i++)
 {
  echo "<br>Value of i Variable is: ". $i;
  if($i == 5){
	break;
  }
 }
?>


Output :

Value of i Variable is: 1
Value of i Variable is: 2
Value of i Variable is: 3
Value of i Variable is: 4
Value of i Variable is: 5

5.) Nested for loops PHP

Nested word indicated one or more for loops inside the main for loop. Nested for loops are also useful to print some star pattern or number pattern in PHP. When we write for loop inside the another for loop that is called nested for loop.

Example:

<?php
for($i=1; $i < 5; $i++)
 {
	echo "<br>Value of first for loop ". $i;
	for($j=1; $j < 5; $j++)
	{
		echo "<br>Value of second for loop ". $j;
	}
 }
?>


Output:

Value of first for loop 1
Value of second for loop 1
Value of second for loop 2
Value of second for loop 3
Value of second for loop 4
Value of first for loop 2
Value of second for loop 1
Value of second for loop 2
Value of second for loop 3
Value of second for loop 4
Value of first for loop 3
Value of second for loop 1
Value of second for loop 2
Value of second for loop 3
Value of second for loop 4
Value of first for loop 4
Value of second for loop 1
Value of second for loop 2
Value of second for loop 3
Value of second for loop 4

More Related Post

Thanks for the reading post. I hope you like and understand the post. If you have any doubt regarding this post please comment below.

Leave a comment

Your email address will not be published. Required fields are marked *