PHP loops

With a PHP loop, you can run the same block of code a certain number of times. PHP has the following four types of loops.

For: for makes a block of code run through itself a certain number of times.

While:  while runs a block of code over and over again as long as a certain condition is true.

do…while : do while goes through a block of code once and then repeats the loop as long as a special condition is true.
foreach : foreach repeats a block of code for every item in an array.
We\’ll talk about the continue and break keywords, which are used to control how loops work.

For loop

When you know how many times you want to run a statement or a block of statements, you use the for statement.

PHP\’s for loop Syntax

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

The initializer is used to set the number of loop iterations counter\’s starting value. For this, you can declare a variable, and the usual name for it is $i.

Example

The code in the next example loops five times and changes the value of two variables each time through the loop.

<html>
<body>

<?php
$x= 0;
$y= 0;

for( $i = 0; $i<10; $i++ ) {
$x += 5;
$y += 2;
}

echo (\”At the end of the loop x = $x and y = $y\” );
?>

</body>
</html>

Output

At the end of the loop x = 50 and y = 20

 

while loop statement

If and for as long as a test expression is true, a block of code will be executed by the while statement.

The code block will be performed if the test expression is true. The test expression will again be evaluated after the code has run, and the loop will keep going until it is determined that the test expression is false.

PHP\’s While loop Syntax

while (condition) {
code to be executed;
}

Example

On each loop iteration in this example, a variable\’s value is decreased, and the counter increases until it hits 10, at which point the evaluation is false and the loop is terminated.

 

<html>
<body>

<?php
$a = 0;
$numbr = 100;

while( $a< 10) {
$numbr –;
$a++;
}

echo (\”Loop stopped at a = $a and number = $numbr \” );
?>

</body>
</html>

 

 

Output

This will lead to the outcome shown below.

At a = 10 and number = 90, the loop halted.

do…while loop Statement

If a condition is true, the do…while statement will run a block of code at least once before repeating the loop.

Syntax

do {
code to execute;
}
while (condition);

 

Example

The example that follows will increase the value of I at least once and keep increasing it until it reaches a value lower than 10 Live Demo

<html>
<body>

<?php
$a = 0;
$number = 0;

do {
$a++;
}

while( $a< 15 );
echo (\”Loop stopped at a = $a\” );
?>

</body>
</html>

 

Output

This will lead to the outcome shown below.

Loop halted at index 15

 

The foreach loop statement

To loop around an array, use the foreach command. The array pointer is shifted one position for each pass, and the value of the current array element is assigned to $value. The following pass will process the next element.

Syntax

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

Example

Try the example in the following to list the values of an array.

<html>
<body>

<?php
$array = array( 1, 2, 3, 4, 5,6,7,8,9);

foreach( $array as $val ) {
echo \”Value is $val <br />\”;
}
?>

</body>
</html>

Output

This will lead to the outcome shown below.

Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
Value is 6
Value is 7
Value is 8
Value is 9

 

The break Statement

To end the execution of a loop early, use the PHP break keyword.

The statement block contains the break statement. You have complete control over it, and you can leave the loop at any time. An immediate statement to the loop will be executed after exiting the loop.

Break Statement PHP Example

When the counter value hits 3 and the loop closes, the condition test in the example below becomes true.

<html>
<body>

<?php
$k = 0;

while( $k< 10) {
$k++;
if( $k == 5 )break;
}
echo (\”Loop stopped at k = $k\” );
?>

</body>
</html>

Result

Loop stopped at i = 3

The continue statement

While it stops the current iteration of a loop, the PHP continue keyword does not end the loop.

The continue statement follows a conditional test and is positioned inside the statement block that contains the code that the loop performs, just as the break statement. The remainder of the loop code is skipped for passes that come across continue statements, and the subsequent pass begins.

Continue Statement PHP Example

The loop in the following example shows the contents of the array, but when the condition is met, it simply skips the relevant code and produces the next item.

<html>
<body>

<?php
$array = array( 0,1, 2, 3, 4, 5);

foreach( $array as $val) {
if( $val == 2 )continue;
echo \”Value is $val <br />\”;
}
?>

</body>
</html>

Output

This will lead to the outcome shown below.

Value is 0
Value is 1
Value is 2
Value is 4
Value is 5

 

 

Related Posts
50+ PHP Interview Questions and Answers 2023

1. Differentiate between static and dynamic websites. Static Website The content cannot be modified after the script is executed The Read more

All We Need to Know About PHP Ecommerce Development

  Many e-commerce sites let you search for products, show them off, and sell them online. The flood of money Read more

PHP Custom Web Development: How It Can Be Used, What Its Pros and Cons Are,

PHP is a scripting language that runs on the server. It uses server resources to process outputs. It is a Read more

PHP Tutorial

Hypertext Preprocessor (PHP) is a programming language that lets web developers make dynamic content that works with databases. PHP is Read more

Introduction of PHP

PHP started out as a small open source project that grew as more and more people found out how useful Read more

Syntax Overview of PHP

This chapter will show you some of PHP\'s very basic syntax, which is very important for building a strong PHP Read more

Environment Setup in PHP

To develop and run PHP on your computer, you need to instal three important parts. Web server PHP can almost Read more

Variable Types in PHP

Using a variable is the main way to store information in the middle of a PHP program. Here are the Read more

Scroll to Top