Home >Backend Development >PHP Tutorial >Introduction to php for loop usage examples, php examples_PHP tutorial

Introduction to php for loop usage examples, php examples_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:51:181009browse

Introduction to php for loop usage examples, php examples

for loop is used when you know in advance the number of times the script needs to be run.

Grammar

for (初始值; 条件; 增量)
{
要执行的代码;
}

Parameters:

  • Initial value: Mainly initializes a variable value, used to set a counter (but can be any code that is executed once at the beginning of the loop).
  • Condition: Restriction conditions for loop execution. If TRUE, the loop continues. If FALSE, the loop ends.
  • Increment: Mainly used to increment the counter (but can be any code that is executed at the end of the loop).

Note: The above initial value and increment parameters can be empty, or have multiple expressions (separated by commas).

The following example defines a loop with an initial value of i=1. As long as the variable i is less than or equal to 5, the loop will continue to run. Each time the loop runs, the variable i will be incremented by 1:

<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br>";
}
?>

Output:

The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

Original address: http://www.manongjc.com/php/php_while.html

Related reading:

php strtok() usage - php strtok() function example explanation

The difference between php string splitting function strtok() and explode()

php strtr() string replacement function usage analysis

php substr() intercepts a substring of specified length starting from the specified position in the string

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1130979.htmlTechArticleIntroduction to php for loop usage example, php example for loop is used when you know in advance the number of times the script needs to be run. Syntax for (initial value; condition; increment) {code to be executed;} Parameters...
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