PHP Loops

PHP Loops

Hope this article finds you well.

Today we will learn about the Loop types available in PHP.

So read the article till the end and i will make sure you know everything about PHP Loops.

Before i proceed, i would request you to do SUBSCRIBE to ShoutMe360.com so that you can get regular updates.

So lets start…..

What is PHP Constant?

“PHP loops execute a block of code untill/unless a specified condition gets reached or true.”

In most of cases, when you write code, you want the same block of code to run over and over again continuously for number of times. In those situations, instead of adding same block of code several times, we can use loops to perform a task like this.

PHP supports following four loop types.

for – loop through a block of code a specified number of times.

while – loop through a block of code if and as long as a specified condition is true.

do…while – loop through a block of code once, and then repeats the loop as long as a special condition is true.

foreach – loop through a block of code for each element in an array.

The for loop statement

The for statement is used when you know how many times you want to execute a statement or a block of statements.

Syntax

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

Parameters:

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

Example

<?php

for ($x = 0; $x <= 10; $x++) {
echo “The number is: $x”;
}
?>

This will displays the numbers from 0 to 10

The while loop statement

The while loop executes a block of code as long as the specified condition is true.

If the test expression is true then the code block will be executed. After the code has executed the test expression will again be evaluated and the loop will continue until the test expression is found to be false.

Syntax
while (condition is true) {
code to be executed;
}

Example

<?php

$x = 1;

while($x <= 5) {
echo “The number is: $x “;
$x++;
}
?>

The above example first sets the value of $x to 1 and set prints the value of $x until/unless its value is less or equal to 5. Also increase the value of $x by 1 with every loop.

The PHP do…while Loop

The do…while loop always execute the block of code once and then checks the condition, and repeat the loop while the specified condition is true.

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

Example

<?php
$x = 1;

do {
echo “The number is: $x
“;
$x++;
} while ($x <= 5); ?>

The example above, first sets a variable $x to 1 ($x = 1). Then, the do while loop will write some output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5:

Note: In a do while loop the condition is checked AFTER executing the statements within the loop. This means that the do while loop would execute its statements at least once, even if the condition is false the first time.

The PHP foreach Loop

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

Syntax

foreach ($array as $key => $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.

<?php

$colors = array(“red”, “green”, “blue”, “yellow”);

foreach ($colors as $key => $value) { echo $key.” => “.$value. “”; }
?>

The above example will display the default index assigned and their corresponding values.
0 => red
1 => green
2 => blue
3 => yellow

Hope this explains you enough about the Loops in PHP.

This is Lalit signing off for today.

If you like this post please give a thumbs up and keep it sharing.

Hey don’t forget to SUBSCRIBE please so that you can get notification on new posts.

Tada

Leave a Reply

Share21
Tweet
+1