PHP 7.x — P29:While循环

If you’ve never been introduced to the concept of a loop, think about it this way. Let’s say that you’re looking at an excel sheet. In that excel sheet you have rows that go down the sheet. Each row has some data inside of it. To read the contents of the next row, you move down. You can keep reading the contents of each row up until you reach the last row of that excel sheet.

如果您从未接触过循环的概念,请这样考虑。 假设您正在查看一张Excel工作表。 在该excel工作表中,您可以在工作表中找到几行。 每行内部都有一些数据。 要阅读下一行的内容,请向下移动。 您可以继续阅读每一行的内容,直到到达该Excel工作表的最后一行。

With a loop, you can read the information from a particular row, add 1 to the current row number, and move to the next row. You can then read the contents of the next row.

通过循环,您可以读取特定行的信息,将1添加到当前行号,然后移至下一行。 然后,您可以阅读下一行的内容。

Instead of looking at an actual excel sheet, we’ll look at an array. The array can be used to model rows and columns of something like an excel sheet.

而不是查看实际的Excel工作表,我们将查看一个数组。 该数组可用于为Excel工作表之类的行和列建模。

We have the following array: $best_cars_ever_made. We’ve already looked at how to display an individual element from the array, but how can we keep displaying element after element, starting at “S13 240sx” and ending at “Subaru STi?”

我们有以下数组: $ best_cars_ever_made 。 我们已经研究了如何显示数组中的单个元素 ,但是如何才能继续显示从“ S13 240sx”开始到“ Subaru STi”的一个又一个元素

We can do that with a loop. There are numerous loops that we’ll cover, but we’re going to be focusing on only one here: the while loop.

我们可以循环使用。 我们将介绍许多循环,但是这里我们只关注一个循环while循环

while ( expression is true ) {
do something
}

The while loop says, “while the expression is true, do something inside of the loop body.” If we passed the value “true” to the conditional expression, the loop would run forever (well, as long as PHP allows it to run before it crashes). Programming languages like Java do allow you to run the loop forever and it’s actually pretty common to see a while(true) loop; in PHP, the always true loop will run until it times out; at that point an error is returned.

while循环说:“当表达式为真时,请在循环体内执行一些操作。” 如果我们将值“ true”传递给条件表达式,则循环将永远运行(只要PHP允许它在崩溃前运行即可)。 像Java这样的编程语言确实允许您永远运行循环,实际上,看到while(true)循环很普遍; 在PHP中, 始终为true的循环将运行直到超时; 那时返回错误。

The big takeaway: make sure that your while loop ends eventually.

最大的收获:确保while循环最终结束。

What kind of expression are we testing? You’ve already done this type of testing with if statements. It’s just a conditional. So you can say something like:

我们正在测试哪种表达方式? 您已经使用if语句完成了这种测试。 这只是有条件的。 因此,您可以说类似以下内容:

  1. while ( $x < 10 )

    而($ x <10)
  2. while ( $name != “Dino” )

    而($ name!=“ Dino”)
  3. while ( $brand == “Ferrari” || $brand == “Porsche” )

    while($ brand ==“法拉利” || $ brand ==“保时捷”)

Inside of the loop body, you have your statements that execute. You also need to have a way to break the conditional expression. For example, if you are testing while( $x < 10 ), the value of $x will eventually have to change to a value that’s greater than or equal to 10 in order for the loop to stop executing.

在循环体内,您可以执行语句。 您还需要一种方法来打破条件表达式 。 例如,如果您正在测试while($ x <10) ,则$ x的值最终将不得不更改为大于或等于10的值,以使循环停止执行。

Let’s look at an example and walk through it.

让我们看一个例子并逐步进行。

<?php$x = 0;while( $x < 2 ) {
echo $x;
$x++;
}?>

In the example above:

在上面的示例中:

  1. PHP assigns the integer value 0 to $x.

    PHP将整数值0分配给$ x

  2. It encounters the while statement.

    它遇到while语句。

  3. PHP tests the conditional. Is the value at $x less than 2? The value that’s currently stored inside of $x is 0. So, is 0 less than 2? Yes. So, move into the loop body.

    PHP测试条件。 $ x的值是否小于2? $ x内部当前存储的值为0。因此,0小于2吗? 是。 因此,进入循环体。

  4. PHP encounters the echo statement and prints out the value of $x, which is 0.

    PHP遇到echo语句并输出$ x的值,该值为0。

  5. The variable $x is incremented from 0 to 1 with the post-increment operator (++).

    使用后递增运算符(++),变量$ x从0递增到1。

  6. PHP returns to the top of the while loop and tests the condition again. Is $x less than 2? Remember, the value of $x was incremented inside of the while loop body. The variable $x now contains the value 1. So, is 1 less than 2? Yes. Move into the loop body.

    PHP返回while循环的顶部并再次测试条件。 $ x小于2吗? 请记住, $ x的值在while循环主体内递增。 变量$ x现在包含值1。因此,1小于2吗? 是。 进入循环体。

  7. Echo the value of $x, which is now 1.

    回显$ x的值,现在为1。

  8. Increment $x. The integer value 2 is now stored inside of $x.

    增加$ x 。 现在,整数值2存储在$ x内

  9. PHP returns to the top of the while loop and tests the condition again. Is $x less than 2? The value of $x was incremented inside of the loop body and now stores the value 2. So, is 2 less than 2? No. Skip the loop body and continue execution after the loop’s closing curly brace.

    PHP返回while循环的顶部并再次测试条件。 $ x小于2吗? $ x的值在循环体内增加,现在存储值2。因此,2小于2吗? 否。跳过循环主体,并在循环的右花括号后继续执行。

  10. The result that’s displayed on the screen is: 01

    屏幕上显示的结果是: 01

We can now apply the same logic to our $best_cars_ever_made array.

现在,我们可以将相同的逻辑应用于$ best_cars_ever_made数组。

We begin by counting the number of elements in the array. There are 11 total elements inside of the $best_cars_ever_made array. We can use that information inside of the loop test. We also know how to print out an individual element from an array: echo $best_cars_ever_made[5]. To print the first array, we need to print the value at index 0. To print the last value, we need to print the value at index 10.

我们首先计算数组中元素的数量。 $ best_cars_ever_made数组中共有11个元素。 我们可以在循环测试中使用该信息。 我们还知道如何从数组中打印出单个元素: echo $ best_cars_ever_made [5]。 要打印第一个数组,我们需要在索引0处打印值。要打印最后一个值,我们需要在索引10处打印值。

So, we initialize a counter variable $i to 0; it’s also going to be used as the array index variable. To print out each array element, we’ll start at 0 and work our way up to 10 since the last element inside of the array is at index 10. We know that we have 11 elements so our condition can be either while($i < 11) or while($i ≤ 10). If we start at 0, we’re going to end at 10 either way.

因此,我们将计数器变量$ i初始化为0; 它也将用作数组索引变量。 要打印每个数组元素,我们将从0开始,一直到10,因为数组中的最后一个元素位于索引10。我们知道我们有11个元素,因此我们的条件可以是while($ i <11)while($ i≤10) 。 如果我们从0开始,那么无论哪种方式都将以10结尾。

Inside of the loop body, we pass the value of $i to the $best_cars_ever_made array and echo out the element that’s returned. Right after that, the variable $i is incremented and the processes is repeated.

在循环体内,我们将$ i的值传递给$ best_cars_ever_made数组,然后回显返回的元素。 此后,变量$ i递增,过程重复。

If we walk through the example:

如果我们看一下示例:

  1. PHP sets $i = 0.

    PHP设置$ i = 0

  2. PHP tests to see whether $i is less than 11.

    PHP测试以查看$ i是否小于11。

  3. Since $i is less than 11, PHP enters the loop body.

    由于$ i小于11,PHP进入循环体。

  4. PHP evaluates the expression $best_cars_ever_made[$i]. The variable $i is replaced with 0.

    PHP计算表达式$ best_cars_ever_made [$ i] 。 变量$ i被替换为0。

  5. PHP grabs the element returned by $best_cars_ever_made[0], which in this case is “S13 240sx,” and echoes it out.

    PHP获取$ best_cars_ever_made [0]返回的元素,在本例中为“ S13 240sx”,并将其回显。

  6. The variable $i is incremented to 1 and the process repeats.

    变量$ i增加到1,然后重复该过程。

  7. This keeps repeating until $i is set to 11. On the 11th attempt, the condition breaks.

    这一直重复直到$ i设置为11。在第11次尝试时,条件中断。

  8. The output will be each vehicle listed one right underneath the other due to the <br> tag.

    由于<br>标签,输出将是每辆车在另一辆车的右下方列出。

If you’re dynamically generating arrays, most of the times the total number of elements in the array is unknown. PHP has a helper function that can aid us with that: count(). The count() function accepts an argument, which is the array, and returns the total number of elements inside of it. In this case, count($best_cars_ever_made) will return 11. We can substitute the count() function for 11 and our while loop conditional statement will be dynamic.

如果要动态生成数组,则大多数情况下,数组中元素的总数是未知的。 PHP有一个辅助函数可以帮助我们: count()count()函数接受一个参数,即数组,并返回其中的元素总数。 在这种情况下, count($ best_cars_ever_made)将返回11。我们可以将count()函数替换为11, 而while循环条件语句将是动态的。

We can simplify the above code even further by moving $i++ inside of the square brackets of the $best_cars_ever_made array. Remember that PHP evaluates the variable first and then increments it after it has been evaluated.

通过将$ i ++移动到$ best_cars_ever_made数组的方括号内,我们可以进一步简化上述代码。 请记住,PHP首先评估变量,然后在评估之后对其进行递增。

One final thing to note is that if the condition is never true, PHP just skips over the while loop. So, if $i was initialized to 20 for the example above, PHP would test to see whether $i was less than 11. Since 20 is not less than 11, PHP would just skip over the loop.

最后要注意的一点是,如果条件永远不会为真,PHP只会跳过while循环。 因此,如果上面的示例将$ i初始化为20,PHP将进行测试以查看$ i是否小于11。由于20不小于11,PHP只会跳过循环。

翻译自: https://medium.com/dev-genius/php-7-x-p29-while-loops-2f178ae3d747

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值