Multiple ways to loop through an array in php

In this article  we will learn how to loop through an array in PHP with all possible ways and all PHP array functions.

To access an element in an array if you do not know index you can use loop to get element of array. On the other hand Loops are backbone of any programming language. what are loops in any programming language ? Loops are used to execute the piece block of code for  number of times.
In this article we will also discuss about continue and break. continue and break are very important concepts statement like how you can skip an iteration in an loop or how you can stop loop execution on the base of some condition.

There are following  four loop types that used in PHP.

 

for - for loops are the most complex loops in PHP. They behave like their C counterparts.

while - while loops are the simplest type of loop in PHP. It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to true

do -- whiledo-while loops are very similar to while loops, except iteration  executes with any condition because do-while loops evaluates  its expression at he end of the loop

foreachforeach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects.

 

continue and break statements in PHP:

The continue statement breaks one iteration in the loop you can call it on the base of some condition like if(a == 5) continue;

The break statement is used in an array  to "jump out" of the loop like if(a == 5) break;


While loop in PHP

The while loop is very common loop among all languages and PHP is not different. In fact, while loop is one of the most popular method to iterate over PHP array.

while(expression){
    // Do your code here
}
$countArray= array(1,2,3,4,5,6);
$i = 0;

while($i < count($countArray))
{
	echo $countArray[$i]."";
	$i++;
}

 

do while Loop in PHP

The do while Loop  would run one time exactly, since after the first iteration, when truth expression is checked, then it decides on the base of expression weather it should continue or not.


do {
	// Code to be executed
}
while(expression);

 

For each loop in PHP

The foreach statement is used to loop through arrays and objects. For each pass the value of the current array element is assigned to $value in each iteration.

//without key
foreach (iterable_expression as $value)
    statement

//with key
foreach (iterable_expression as $key => $value)
    statement
    
// Example
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}

 

for loop in PHP

The for statement is used when want to execute some piece of code according  required number of times. I the follwing example you will also learn syntax of continue and break statements in PHP. Like how you can skip and iteration and and how you can jump out of the loop

for (expr1; expr2; expr3)
    statement;

// example
for ($i = 1; $ < 15 ; $i++) {

	//skip this iteration

	if ($i > 5) {
        continue;
    }
    
    // jump out of the loop
    if ($i > 10) {
        break;
    }
    echo $i;
}

Tags:

PHP

Share:

Related posts