PHP Loops
PHP Tutorial

PHP Loops

 

Loop is a sequence of instructions continuously repeated depending on the boolean condition. If we want to execute our statement repeatedly, we can implement loop techniques and put statements execution according to the boolean condition.

PHP Loop Types

While Loop

For Loop

Do While

PHP While Loop

While loop is the most accessible loop to implement re-execution statements continuously while the pre-specified condition is true. Loop depends on three major steps one initialization of the counter and second condition and the last one increment or decrement counter for testing condition.

Syntax of While Loop

       while (condition)

       {

              Statements;

              Increment or Decrement;

       }

Example of While Loop

<?php

// Example of while loop by xcnotes.com

// repeat continuously until counter becomes 10

// output: ‘xxxxxxxxx’

$counter = 1;

       while ($counter < 10)

       {

              echo ‘x’;

              $counter++;

       }

?>

while loop in php

PHP For Loop

For loop used to re-execute coding block on specified steps that we are aware of it. It executes the coding block on specified steps.

PHP For Loop Syntax

       for ( Counter Initialization; Condition ; Increment/decrement )

       {

              Statements;

              Increment or Decrement;

       }

PHP For Loop Example

<?php

// Example by xcnotes.com

// Table Printing of given no to $num variable

// output: ‘2 4 6 8 10 12 14 16 ‘

$num = 2;

       for ($counter=1;$counter<=10;$counter++)

       {

              $table=$counter*$num;

              echo “$table <br/>”;

       }

?>

For loop in PHP

PHP Do While Loop

Do while loop is so different from for or while loop. It executes statements at least once, and then repeatedly executes the code block or not depending on the boolean condition.

PHP Do While Loop Syntax

do

{

       Statements;

       Increment or Decrement;

}

while(condition);   

PHP Do While Loop Example

<?php

// Example by xcnotes.com

// repeat continuously until counter becomes 10

// output: ‘xxxxxxxxx’

$counter = 1;

       do

       {

              echo ‘x’;

              $counter++;

       }

       while ($counter < 10);

?>          

                            

DO WHILE in PHP

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment