EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP Loops

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP Loops

PHP Loops

Introduction to PHP Loops

PHP Loops are a type of code which can help us to run some code inside of the loop to run over and over again according to our requirement as the input and these loops will help run the code and complete the task infinitely as we want in order to execute the same code inside of the loop again and again until our condition becomes false or else the code runs continuously. The word itself says that it is going to be repeated but only if a certain condition is true which is mentioned in the loop parameters to check the condition for the PHP loop/loops.

Different Loops of PHP

Just like the other programming languages, PHP programming language also offers different types of loop concepts. They are: WHILE LOOP, DO WHILE LOOP, FOR LOOP, FOREACH LOOP. You will get a detailed explanation of each and every loop concept of PHP below.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

different types of Loops

1. While Loop

While Loop will run the specific/ some block of code which is inside of the while loop parenthesis of PHP only if the condition mentioned in the loop is true. If the condition is false the While Loop will break the code which is in the continuous process of running the code.

Syntax:

While(Condition to check){
//Code which is need to executed or the code statements which is to run
}

Explanation:

In the above syntax, while loop is mentioned with the condition inside of the parenthesis to run statements inside of the loop only if the condition mentioned is True or else the code inside of the loop will not run by breaking the loop get out of the loop/while loop.

For Example:

The below example consists of while loop programming in order to print the list of numbers from 1 to 10. Here in the below example, variable 1 is assigned with the number 1 and then the loop program started with the help of $i variable value and the while loop. While Loop started with the condition i<=10 to check whether $i variable value is less than “10” then code will be executed which is inside only if the condition is True. Loop will run continuously and prints the values and then the $i value will be incremented by 1 and then breaks the loop when the $i variable value becomes “11” because the condition $i<=10 became false. This Program is like printing the natural numbers from 1 to 10 values.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,731 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Code:

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

Output:

PHP loops 1

2. Do While Loop

Do While Loop is just like the While Loop but in Do While Loop condition will not be checked at first whereas in While Loop condition will be checked first and then the programming code inside of the loop will run.

Syntax:

do{
//Programming statements which is need to be executed only if the loop condition is true
}
While(condition to check);

For Example:

The below program contains 2 do while programs in order to print the list of even numbers between 1-10 and the list of odd numbers between 1-10. The program also prints the sum of odd numbers, even numbers and also the sum of all the natural numbers which are between 1-10. In the 1st do-while loop the $i variable value is checked whether the value is fully divided by the value “2”. If True then the value will be printed and $k value will store the $i value else nothing happens just the incrementation of the $i variable value. Likewise, the loop continues until the $i value will reach the value “10”. Just like that, others do while loop also runs by checking whether the $j value will not be divided by 2 value or not. If True then $j value will be printed and the $m will store the value. At last, the $k will store the sum of the even numbers and $l will store the sum of odd numbers. $m will store the sum of all the natural numbers and those values will be printed as shown in the pic in the output.

Syntax:

<?php
$i = 1;
echo "List of Even Numbers between 1-10:: ";
$k = 0;
$m = 0;
do{
if($i%2==0){
echo " $i " ." , ";
$k=$k+$i;
}
$m=$m+$i;
$i=$i+1;
}while($i <= 10);
echo "<br>"." Sum of the total even numbers between 1-10 ::"." $k";
echo "<br>";
$j = 1;
$l = 0;
echo "List of the ODD Numbers between 1-10:: ";
do{
if($j%2!=0){
echo " $j " ." , ";
$l=$l+$j;
}
$j=$j+1;
}while($j <= 10);
echo "<br>"." Sum of the total odd numbers between 1-10 ::"." $l";
echo "<br>";
echo "<br>"." Sum of the total natural numbers between 1-10 ::"." $m";
echo "<br>";
?>

Output:

list of even numbers

3. For Loop

For Loop is somewhat different when we compare with the While Loop and the Do While Loop. It executes the code again and again if the certain true condition is met. Loop will run the code for certain for the number of times as we needed and it is controlled with the condition.

For Loop will have 3 parameters. They are initialization, condition, and the incrementation value inside of the For Loop parenthesis.

Parameters of the For Loop:

  • Initialization: In For Loop, this is the value/variable value to start the program.
  • Condition: In For Loop, this is the value/variable value which is needed to be checked. If the condition becomes true then the program statements will run continuously by checking the condition.
  • Incrementing/Incrementation: In For Loop, the initial value or running value in the program statements will be incremented by 1 or other as we needed based on our requirement.

Syntax:

for(initialization value; condition value; incrementing value){
//Programming code statements which is need to be executed when condition of the loop becomes TRUE
}

For Example:

The below for loop example will print the list of the natural numbers which are between 1-30 and also the sum of all the values which are between 1-30.

$i is assigned as 1 at first as an initialization value, condition as $i less than or equal to 30 with the incrementation as $ii=$i+1. For Loop will print $i value until the i value becomes 30 and $j variable’s value will store all the numbers/values of $i variable and then it will sum them one by one in the loop until the I value reaches to 30. Then the printing of the sum of all the natural numbers between 1-30 will be printed after printing all the natural numbers using the For Loop.

Syntax:

<?php
echo "List of the Natural Numbers between 1-30 :: ";
$j=0;
for($i=1; $i<=30; $i++){
echo "$i" . " , ";
$j=$j+$i;
}
echo "<br>";
echo "Sum of all the natural numbers between 1-30 :: ";
echo "$j";
echo "<br>";
?>

Output:

list of the natural numbers

4. Foreach Loop

Foreach is one of the loop concepts of PHP which is used to iterate over the array/arrays.

Syntax:

foreach($array as $value){
//Programming code which is need to be executed
}

For Example:

The below example will print the values from the $colors1 variable. $colors1 variable values are the list of the colors. Using the foreach loop concept will print the colors which are present in the array one by one.

Syntax:

<?php
$colors1 = array("Yellow", "Red", "Blue", "Green",);
// Colors array accessing in the loop
foreach($colors1 as $value1){
echo $value1 . "<br>";
}
?>

Output:

php loops 4

Recommended Articles

This is a guide to PHP Loops. Here we discuss the introduction and different loops of PHP with parameter, examples, and syntax. You may also have a look at the following articles to learn more –

  1. if-else statement in PHP
  2. Star Patterns in PHP
  3. PHP Superglobal Variables
  4. PHP Array Search

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More