Home >Backend Development >PHP Tutorial >PHP basics: Detailed introduction to the for statement in PHP
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.
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.
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
$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!