Home  >  Article  >  Backend Development  >  PHP basics: Detailed introduction to the for statement in PHP

PHP basics: Detailed introduction to the for statement in PHP

不言
不言Original
2018-12-28 10:58:215165browse

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(初始值;条件表达式;递增/递减表达式){ 
            执行处理
}

() 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;
}
?>

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;
}
?>

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!

Statement:
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
Previous article:What is Yii frameworkNext article:What is Yii framework