Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to figure out the While loop and do-while loop of PHP

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to figure out the While loop and do-while loop of PHP". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to figure out the While loop and do-while loop of PHP.

PHP cycle-While cycle

First of all, why is there a cycle? Why does the code use loops? In our daily use, we will encounter the need to run the same code block repeatedly. Instead of adding several almost equal lines of code, we can use loops to perform such tasks.

What is a While loop?

While loop is a kind of PHP loop, while is a Boolean loop, it will first determine the value of while (Boolean judgment) is true (true) and execute the code enclosed in curly braces. If it is false, go out and execute the following code.

The format of the While syntax is as follows:

While (condition) {Action;}

The evaluation of the "condition" in the while loop must be true (TRUE) or false (FALSE) of Boolean type, and if it is a value of other type, it will be automatically converted to a value of Boolean type.

Examples are as follows:

Output result:

It can be seen from the results that the While loop will stop only if the result of the While loop condition is flase.

PHP do...while cycle

You've learned a lot about the While loop, so let's take a look at the do...while loop.

What is a do...while loop?

The two are very similar. Let's take a look at the difference: the expression of the do while loop is not checked at the beginning of each loop, but at the end of each loop.

Regardless of whether the while judgment is true or not, do-while first executes a code block loop statement to ensure that it will be executed once, and then determines it.

The syntax of the do...while loop is as follows:

Do {Action;} while (condition)

The evaluation result of "condition" in the while statement must also be Boolean true (TRUE) or false (FALSE).

When using do while, you must end up with a semicolon;.

Output result:

You can try to make the initial $an assignment false, that is, it does not meet the do-while condition, to see how the output changes:

Output result:

Since the judgment is false, the do-while loop will not start and the action chunk will only be executed once.

To sum up:

The flow of the do while loop is: first execute the action in the body of the loop, then judge the value of the conditional expression, when the value of the input condition is TRUE, return to re-execute the action statement in the body of the loop, and so on, until the value of the expression is equal to FALSE, the loop ends.

At this point, I believe you have a deeper understanding of "how to figure out the While loop and do-while loop of PHP". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report