For Loop Java

For Loop in PHP

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 *

0 0 0
FOLLOW ON