Home > Backend Development > PHP Problem > What are the loop statements in php

What are the loop statements in php

(*-*)浩
Release: 2023-02-23 18:48:01
Original
6518 people have browsed it

What are the loop statements in php

for loop

Grammar rules:

for(初始化计数初值;判断条件;增加计数值){
循环体;
}
Copy after login

Example:

What are the loop statements in php

Execution results: (Recommended learning: PHP programming from entry to proficiency)

0 1 2 3 4
Copy after login
Copy after login
Copy after login

while loop

Grammar rules

while(判断条件){
循环体;
}
Copy after login

The while loop first judges the loop condition and then loops. When the condition is not met, break out of the loop.

Note: Increase the count value, otherwise it will enter an infinite loop.

Example:

What are the loop statements in php

Execution result:

0 1 2 3 4
Copy after login
Copy after login
Copy after login

do while loop

Grammar rules

do{
循环体;
}while(判断条件);
Copy after login

do while loop first enters the loop and then determines the loop condition. So no matter whether the judgment condition is true or false, there will be at least one loop.

Example:

What are the loop statements in php

Execution result:

0 1 2 3 4
Copy after login
Copy after login
Copy after login

foreach loop

Grammar rules

foreach($array as $value){
执行代码;
}
Copy after login

or

foreach($array as $key => $value ){
执行代码;
}
Copy after login

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

foreach is used for array traversal. Each time a loop is performed, the current value is assigned to the $value variable, and the array pointer moves one by one until the last element.

Example:

What are the loop statements in phpExecution result:

What are the loop statements in php

The above is the detailed content of What are the loop statements in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template