LOOPS in PHP tutorial

PHP is very powerful language handling many major projects. It have inbuilt many decision making tools One of them is LOOPS.
when dealing with programming there many times we need to run that code many times or untill the condition meet. this can be achieved by LOOPING statements.
There are few loops in php likewise:

  1. While Loop
  2. Do…While Loop
  3. For Loop
  4. Foreach Loop
  5. Break and Continue Statements

Loops in PHP

While Loop

While statement executes a block of code if and as long as a specified condition is true. If the condition becomes false, the statements within the loop stop executing.

while (condition)
{
code to be executed;
}

code associated with the While statement is always enclosed within the { opening and } closing brace symbols to tell PHP clearly which lines of code should be looped through.

While condition is often used when we don’t know how many time code to be excute. Like when displaying the records of users of showing a list.

Let’s have a look at the examples. The first example defines a loop that starts with i=0. The loop will continue to run as long as the variable i is less than, or equal to 10. i will increase by 1 each time the loop runs:

<?php
$i=0;
while ($i <= 10) { // Output values from 0 to 10
echo "The number is ".$i."<br />";
$i++;
}
?>

Note: Make sure the condition in a loop eventually becomes false; otherwise, the loop will never terminate.

Do…While Loop

Do…While statements are similar to While statements, except that the condition is tested at the end of each iteration, rather than at the beginning. This means that the Do…While loop is guaranteed to run at least once. The Do…While loop syntax is as follows:

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

The example below 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 or equal to 10:

<?php
$i = 0;
do {
echo "The number is ".$i."<br/>";
$i++;
}
while ($i <= 10);
?>

For Loop

For loop used when we already know that how many times code to be execute.
For loop syntax is as follows:

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

The For statement takes three expressions inside its parentheses, separated by semi-colons. When the For loop executes, the following occurs:

The initializing expression is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of any degree of complexity.
The condition expression is evaluated. If the value of condition is true, the loop statements execute. If the value of condition is false, the For loop terminates.
The update expression increment executes.
The statements execute, and control returns to step 2.

Have a look at the very simple example that prints out numbers from 0 to 10:

<?php
for ($i=0; $i <= 10; $i++)
{
echo "The number is ".$i."<br />";
}
?>

Foreach Loop

Foreach loop is a variation of the For loop and allows you to iterate over elements in an array. The Foreach loop syntaxes are as follows:

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

The example below decscribe the Foreach loop that will print the values of the given array:

<?php
$name = array('Vijay', 'ajay');
foreach ($name as $value) {
echo "Your name ".$value."<br />";
}
?>

Break and Continue Statements

Few times you may want to let the loops start without any condition, and allow the statements inside the brackets to decide when to exit the loop. There are two special statements that can be used inside loops: Break and Continue.

Break statement terminates the current While or For loop and continues executing the code that follows after the loop (if any). Optionally, you can put a number after the Break keyword indicating how many levels of loop structures to break out of. In this way, a statement buried deep in nested loops can break out of the outermost loop.

Examples below show how to use the Break statement:

<?php
Example of using the Break statement:
for ($i=0; $i<=10; $i++) {
if ($i==3){break;}
echo "The number is ".$i;
echo "<br />";
}

The Continue statement terminates execution of the block of statements in a While or For loop and continues execution of the loop with the next iteration:

<?php
Example of using the Continue statement:
for ($i=0; $i<=10; $i++) {
if (i==3){continue;}
echo "The number is ".$i;
echo "<br />";
}
?>

Vijay maurya

Vijay maurya is a web developer having several years experience and passionate about programming. Vijay worked in many project along with reputed companies. The main aim of blogging is to spread knowledge who need help in development and also to improve knowledge and skills.

Leave a Reply

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