Get the App
SLTechnology News&Howtos  ›  Development  › 

How many kinds of loop statements are there in php

Shulou Source: shulou.com Published: 2022-06-01 05:58:15 10月02日 Update

Editor to share with you how many kinds of circular sentences in php, I believe most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

There are four kinds of loop statements. They are: 1, for loop, syntax "for (initial value; condition; added value) {loop body}"; 2, dowhile loop, syntax "do {loop body} while (condition)"; 3, while loop, syntax "while (condition) {loop body}"; 4, foreach loop.

This article operating environment: Windows10 system, PHP7.1 version, Dell G3 computer.

There are several kinds of loop statements in php

In PHP, the following loop statements are provided:

1. While-executes the code block in a loop as long as the specified condition is true

2. Do...while-first execute the code block once, and then repeat the loop when the specified condition holds

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

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

Examples are as follows:

For cycle

Grammar rules:

For (initialization count initial value; judgment condition; increase count value) {loop body;}

While cycle

rule of grammar

While (judgment condition) {cyclic body;}

The while cycle first judges the cycle conditions, and then carries on the cycle. When the conditions are not met, jump out of the cycle.

Do while cycle

rule of grammar

Do {cyclic body;} while (judgment condition)

The do while loop first enters the loop and judges the conditions of the loop. So no matter whether the judgment condition is true or false, there is at least one cycle.

Foreach cycle

rule of grammar

Foreach ($array as $value) {execute code;}

Or

Foreach ($array as $key = > $value) {execute code;}

$array: array $value: array key value $key: array key name.

Foreach is used for array traversal. Loop one at a time so that the current value is assigned to the $value variable, and the array pointer moves one by one to the last element.

The above is all the contents of the article "how many kinds of cyclic sentences are there in php". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Loops conditions syntax code loop bodies arrays statements rules articles elements content not much initial values variables added values most few pointers more times Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Docker MySQL Microsoft Xiaomi