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

What is the execution order of for loops in PHP?

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail about the order of execution of the for loop in PHP. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Execution order: 1, execute the "initialization expression"; 2, execute the "conditional judgment expression", if the value of the expression is true, then execute the "loop body", otherwise end the cycle; 3, after the execution of the loop body, execute the "variable update expression"; 4, after the variable update, enter the next cycle until the conditional judgment value is false, the end of the cycle.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

For loop is the most complex loop structure in PHP. The for loop statement can loop according to the known number of loops, which is suitable for cases where the number of times of execution is clearly known.

The syntax format of the for loop is as follows:

For (initialization expression; conditional judgment expression; variable update expression) {loop body;}

The parameters are described as follows:

Initialization expression: usually used to declare the initial value of a counter, that is, the value at the beginning of the loop

Conditional judgment: used to control whether the code in the body of the loop is executed, and exit the loop immediately if the condition is FALSE

Variable update: every time the loop is executed, the value of the counter is modified immediately

Loop body: some code that needs to be executed when the condition is judged to be true.

The running process of the for loop is:

1) execute the initialization expression first.

2) then execute the conditional judgment expression, and if its value is true (non-zero), the loop body is executed, otherwise the loop ends.

3) execute the "variable update expression" after executing the loop body, increasing or decreasing.

4) repeat steps 2) and 3) until the value of "expression 2" is false, the loop ends.

In the above steps, 2) and 3) are a loop that will be executed repeatedly, and the main purpose of the for statement is to continue to execute steps 2) and 3).

The execution process is shown in the following figure:

Example: calculate the factorial of 100 through the for loop

Output result:

This is the end of the article on "what is the order of execution of for loops in PHP". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

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