Files
freeCodeCamp/guide/chinese/php/loops/for-loop/index.md
Randell Dawson 0a1eeea424 fix(guide) Replace invalid prism code block names (#35961)
* fix: replace sh with shell

fix replace terminal with shell

fix replace node with js

fix replace output with shell

fix replace cs with csharp

fix replace c++ with cpp

fix replace c# with csharp

fix replace javasctipt with js

fix replace syntax  with js

fix replace unix with shell

fix replace linux with shell

fix replace java 8 with java

fix replace swift4 with swift

fix replace react.js with jsx

fix replace javascriot with js

fix replace javacsript with js

fix replace c++ -  with cpp

fix: corrected various typos

fix: replace Algorithm with nothing

fix: replace xaml with xml

fix: replace solidity with nothing

fix: replace c++ with cpp

fix: replace txt with shell

fix: replace code with json and css

fix: replace console with shell
2019-05-15 19:08:19 +02:00

76 lines
1.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: For Loop
localeTitle: 对于循环
---
## 对于循环
PHP `for`语句由三个表达式和一个语句组成:
`for ((initialization); (condition); (final-expression)) statement`
### 描述
* 初始化
* 在循环上第一次执行之前运行。
* 此表达式通常用于创建计数器。
* 这里创建的变量的范围是循环。循环完成后,执行它们就会被销毁。
* 条件
* 在每次迭代执行之前检查的表达式。
* 如果省略,则此表达式的计算结果为`true`
* 最终表达
* 每次迭代后运行的表达式。
* 通常用于增加计数器。
* 但它可以用来运行任何表达式。
* 声明
* 在每次循环迭代中重复的代码。
可以省略这三个表达式或语句中的任何一个。
表达式可以包含用逗号分隔的多个表达式。
condition表达式中将评估所有逗号分隔的表达式。
结果从最后一个获得。
For循环通常用于计算一定数量的迭代以重复语句。
### 常见的陷阱
#### 超过数组的边界
当多次索引数组时很容易超出数组的边界例如尝试引用3元素数组的第4个元素
```php
// This will cause an error.
// The bounds of the array will be exceeded.
$arr = array(1,2,3);
for ($i = 0; $i <= count($arr); $i++) {
var_dump($arr[$i]);
}
```
这将输出:
```shell
int(1) int(2) int(3) NULL
```
有办法修复此代码。
将条件设置为`$i < count($arr)``$i <= count($arr) - 1`
#### 性能问题
上面的代码可能会变慢,因为在每次迭代中都会获取数组大小。
为了解决这个问题,可以将数组大小放入变量中。
```php
//create the $size variable with a second expression comma separated
for ($i = 0, $size = count($arr); $i < $size; ++$i) {
```
### 更多信息
* [PHP.net - 控制结构](https://secure.php.net/manual/en/control-structures.for.php)