Get the App
SLTechnology News&Howtos  ›  Network Security  › 

The usage of While Loop in PHP

Shulou Source: shulou.com Published: 2022-06-01 00:53:39 09月14日 Update

This article mainly introduces "the usage of While loop in PHP". In daily operation, I believe that many people have doubts about the usage of While loop in PHP. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the usage of While loop in PHP". Next, please follow the editor to study!

PHP cycle-While cycle

Loop the number of times the code block is specified, or loop through the code block when the specified condition is true.

.

PHP cycle

When you write code, you often need to have the same block of code run over and over again. We can use loop statements in our code to accomplish this task.

In PHP, the following loop statements are provided:

Http://www.iis7.com/b/wzjk/

While-Loops the block of code as long as the specified condition is true

Do...while-executes the code block once, and then repeats the loop when the specified condition holds

For-the number of times that the code block is executed in a loop

Foreach-loops the code block based on each element in the array

.

While cycle

The while loop executes the code block repeatedly until the specified condition is not established.

Syntax:

While (conditional)

{

Code to be executed

}

Example:

The following example first sets the value of the variable I to 1 ($item1;).

Then, the while loop will continue to run as long as I is less than or equal to 5. Each time the loop runs, I increments by 1:

Output:

The number is 1

The number is 2

The number is 3

The number is 4

The number is 5

.

Do...while statement

The do...while statement executes the code at least once, then checks the condition, and repeats the loop as long as the condition is established.

Syntax:

Do

{

Code to be executed

}

While (conditional)

Example:

The following example first sets the value of the variable I to 1 ($item1;).

Then, start the do...while loop. The loop increments the value of the variable I by 1 and then outputs it. First check the condition (I is less than or equal to 5), as long as I is less than or equal to 5, the loop will continue to run:

Output:

The number is 2

The number is 3

The number is 4

The number is 5

The number is 6

At this point, the study of "the use of the While loop in PHP" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Loops code conditions statements learning running variables output examples instances more times syntax face-to-face help check practical same next task Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Docker MySQL Shulou Technology OPPO Reno