PHP basics: Detailed introduction to the for statement in PHP

不言
Release: 2023-04-04 15:26:01
Original
5215 people have browsed it

PHP will use for loop statements or while loop statements when executing loops. This article will first introduce to you the detailed usage of for loop statements in PHP.

PHP basics: Detailed introduction to the for statement in PHP

The for statement uses a variable called a counter to control the loop process, as written in the following format. Each of the three elements in

for(初始值;条件表达式;递增/递减表达式){ 
            执行处理
}
Copy after login

() has the following meaning.

Initial value - the first value set

Conditional expression for loop processing - what condition you want the loop to process

Increase/decrease expression - each processing How many values ​​should be increased or decreased

Let’s write it in detail

Weset the variable name$i

<?PHP basics: Detailed introduction to the for statement in PHP
for($i = 0; $i < 5; $i++){
echo $i;
}
?>
Copy after login

This means "start from 0 (initial value), up to a value less than 5 (condition), increment by 1 (increment/decrement)".

Therefore, if you use echo to call this program, the following results will be displayed.

PHP basics: Detailed introduction to the for statement in PHP

Now let us specify the condition as 20 values ​​

<?PHP basics: Detailed introduction to the for statement in PHP
for($i = 0; $i <= 20; $i++){
echo $i;
}
?>
Copy after login

The following results will be displayed

PHP basics: Detailed introduction to the for statement in PHP

$i

Summary: This article is all over here, we will introduce it in the next article The contents of the while loop statement in PHP basics: Detailed introduction to the for statement in PHP! ! !

The above is the detailed content of PHP basics: Detailed introduction to the for statement in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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