PHP Loops
PHP Loops are a type of code that 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 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 says that it will be repeated only if a certain condition is true, which is mentioned in the loop parameters to check the condition for the PHP loop/loops.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Different Loops of PHP
Like the other programming languages, PHP offers different 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.
1. While loop
While loop will run the specific/ some block of code inside the while loop parenthesis of PHP only if the condition mentioned in the loop is true; if 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, a 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.
Example:
The example below consists of while loop programming to print the list of numbers from 1 to 10. In the example below, variable 1 is assigned with the number 1, and then the loop program starts with the help of the $i variable value and the while loop. While loop started with the condition i<=10 to check whether the $i variable value is less than “10,” then code will be executed, which is inside only if the condition is True. The 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.
Code:
<?php $i = 1; while($i <= 10){ echo " Now The number is " . $i . "<br>"; $i=$i+1; } ?> </p> <p><strong>Output:</strong></p> <p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172490646384176.jpg" class="lazy" alt="PHP Loops" ></p> <h4 id="Do-While-Loop">2. Do While Loop</h4> <p>The Do While loop executes the programming code inside the loop first and then checks the loop condition, while the While Loop checks the loop condition before running the code inside the loop.</p> <p><strong>Syntax:</strong></p> <pre class="brush:php;toolbar:false">do{ //Programming statements which is need to be executed only if the loop condition is true } While(condition to check);
Example:
The below program contains 2 do while programs 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 the sum of all the natural numbers between 1-10. The first do-while loop checks the value of the variable $i to see if it is completely divisible by the value “2”. If True, then the value will be printed, and the $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 reaches the value “10”. Like that, others do while loop also runs by checking whether the $j value will not be divided by 2 values. If True, the $j value will be printed, and the $m will store the value. Finally, store the sum of even numbers in the variable $k and store the sum of odd numbers in the variable $l. Also, store the total sum of all natural numbers in the variable $m. Display these values in the output, as shown in the picture.
Code:
<?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:
3. For loop
For loop is somewhat different when comparing the While Loop and the Do While Loop. The code will execute repeatedly if it meets a specific condition. The loop executes the code multiple times based on a specified condition.
For loop will have 3 parameters. They are initialization, condition, and the incrementation value inside the For Loop parenthesis.
Syntax:
for(initialization value; condition value; incrementing value){ //Programming code statements which is need to be executed when condition of the loop becomes TRUE }
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 that must be checked. If the condition becomes true, then the program statements will run continuously by checking the condition.
- Incrementing/Incrementation: The program will increment the initial or running value of the program statements by 1 or another value as required, depending on our needs, in a For loop.
Example:
The below for loop example will print the list of the natural numbers between 1-30 and the sum of all the values between 1-30.
To begin, we set $i as 1 for the initial value. The condition is that $i should be less than or equal to 30, with an increment of $i+1. For loop will print the $i value until the i value becomes 30, and the $j variable’s value will store all the numbers/values of the $i variable and then sum them one by one in the loop until the I value reaches 30. After printing all the natural numbers using the For Loop, the sum of all natural numbers between 1-30 will be displayed.
Code:
<?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:
4. Foreach Loop
PHP uses the “foreach” loop concept to iterate over arrays or multiple arrays.
Syntax:
foreach($array as $value){ //Programming code which is need to be executed }
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 in the array individually.
Code:
<?php $colors1 = array("Yellow", "Red", "Blue", "Green",); // Colors array accessing in the loop foreach($colors1 as $value1){ echo $value1 . "<br>"; } ?>
Output:
The above is the detailed content of PHP Loops. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
