Loop in PHP

The loop in PHP is used to execute the same block of code some time specified.

The loop in PHP is used to execute the same block of code some time specified. PHP supports the following 4 types of loops:

  1. For loop - Repeat through a block of code some times identified.
  2. While loop - iterates over a code block if and as long as a condition is defined as true.
  3. Do . while - loop iterates through a code block once, and then repeats that loop as long as a condition is true.
  4. Foreach loop - Repeats through a code block for each element in an array.

At the end of the chapter, we will discuss the keywords continue and break to control the execution of loops in PHP.

For loop in PHP

The for command is used when you know in advance how many times you want to execute a command or a block of commands in PHP.

Loop in PHP Picture 1Loop in PHP Picture 1

Syntax

for ( khởi_tạo ; điều_kiện ; tăng_giảm_giá_trị ) { phần code để thực thi }

The initialization is used to set the start value for the iteration count variable. A variable can be declared here for this purpose and its traditional name is $ i.

For example

The following example creates 5 iterations and changes the assigned value of the two variables each iteration.






Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:

Loop in PHP Picture 2Loop in PHP Picture 2

While loop in PHP

The while statement in PHP will execute a code block if and as long as a condition is defined as true.

If the condition is true, then the code block will be executed. After the code is executed, the conditional expression will be evaluated again, and the loop continues executing until the conditional expression is false.

Loop in PHP Picture 3Loop in PHP Picture 3

Syntax

 while (thing_kater) 
{
The code is executed if the condition is true
}

For example

The following example reduces the value of a variable through each iteration and increases the variable value counting until it reaches 10, then the estimate is false and the loop ends.






Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:

Loop in PHP Picture 4Loop in PHP Picture 4

Do . while loop command in PHP

The do . while command will execute a code block at least once. It will then repeat the loop as long as the condition is true.

Syntax

 by 
{
code to execute
}
while (thing_kien);

For example

The following example will output the value of i at least once, and then it will continue to increase variable i as long as it has a value less than 10.






Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:

Loop in PHP Picture 5Loop in PHP Picture 5

Foreach loop command in PHP

The foreach loop command is used to iterate over an array. For each iteration, the value for the current array element is assigned to $ value and the array pointer is moved to the next element and in the next iteration the next element is processed.

Syntax

 foreach (array as value) 
{
code to execute

}

For example

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




";
}
?>

Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:

Loop in PHP Picture 6Loop in PHP Picture 6

Break command in PHP

Break keywords in PHP are used to end the execution of a sudden loop.

Break command is placed inside the block. It gives you complete control whenever you want to exit the loop. After exiting the loop, the command immediately after that loop will be executed.

Loop in PHP Picture 7Loop in PHP Picture 7

For example

The following example checks the true condition when the variable value counts = 3 and the loop ends.






Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:

Loop in PHP Picture 8Loop in PHP Picture 8

The continue command in PHP

The continue keyword in PHP is used to stop the current loop but not the end of that loop.

Like the break statement, the continue statement is placed inside the code block containing the code that the loop executes, preceded by a conditional test expression. When the continue statement is encountered, the rest of the loop is ignored and the next loop continues.

Loop in PHP Picture 9Loop in PHP Picture 9

For example

The following loop prints out the value of an array, if it satisfies the condition, it prints, otherwise omits and continues the loop.




";
}
?>

Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:

Loop in PHP Picture 10Loop in PHP Picture 10

Follow tutorialspoint

Previous lesson: if, else, switch commands in PHP

Next post: Array (Array) in PHP

4 ★ | 1 Vote