【问题标题】:PHP For Loop ConfusionPHP 循环混淆
【发布时间】:2011-04-08 02:10:02
【问题描述】:

我是编码世界的新手,这是我第一次了解循环。我在一个网站上看到了一个例子,结果完全被弄糊涂了。

/* Sample Code 1 */

$counter=0
$start=1

for($start;$start<11;start++) {
   $counter=$counter+1;
   print $counter;
}

这给了我 1,2,3,4,5,6,7,8,9,10 的结果

现在如果我更新代码如下

/* Sample Code 2 */

$counter=11;
$start=1;

for($start;$start<11;start++) {
   $counter=$counter+1;
   print $counter;
}

这给了我结果 12,13,14,15,16,17,18,19,20

但是,如果我按如下方式更新代码

/* Sample Code 3 */

$counter=11;
$start=1;

for($start;$start<11;start++) {
   $counter=$counter-1;
   print $counter;
}

我得到结果 10,9,8.7.6,5,4,3,2,1

如有错误请指正

如果变量 $counter 的值为 11,我基本上是在代码 $counter=$counter+1 中的 11+1 处开始递增。对吗?

但让我感到困惑的是,如果我在 FOR 循环中的最终值为 $start

【问题讨论】:

  • 你把 counterstart 混淆了。
  • SLaks 所说的。 $start 必须小于 11,并且在循环结束时,即使您正在递增 $counter$start 不是特殊变量或任何东西,它被用作循环中的条件的唯一原因是您在 for(...) 行中以这种方式指定了它。
  • 您确定示例代码 2 中的结果还是只是拼写错误。它应该有 21 作为最后一个值。 $start
  • 抱歉打错了。有一个 21 但这并没有引起我的困惑

标签: php loops for-loop


【解决方案1】:

开始循环时,$start 小于 11。然后在迭代结束时递增。然后,如果达到 11,则循环结束。

即如果$start为10,那么就会进入循环。它到达 11,所以for 语句退出循环。循环结束时为 11。

【讨论】:

  • 记住,逻辑上和while($start&lt;11){$start = $start + 1;}是一样的
【解决方案2】:

这是我写的描述

for (//declare loop
$start = 0; //declare starting value and the value to store it in
$start < 10; // Each time it comes through, if $start is under 10, do the loop. if it is 10, exit
$start++ //Increment $start by 1
)

【讨论】:

    【解决方案3】:

    您的第二个示例结果中似乎缺少“21”。 这可能是您困惑的原因吗?

    【讨论】:

    • 抱歉打错了。有一个 21 但这并没有引起我的困惑
    • 那么正如每个人所说的那样,您可能只是将迭代器与计数器混淆了。除非您能详细说明造成混乱的原因。
    【解决方案4】:

    我会从上面的所有例子中给你一个解释:

    示例代码 1

    $counter=0;
    $start=1;
    

    这是声明和初始化两个变量的变量声明。

    for($start;$start<11;start++) {
        $counter=$counter+1;
    

    for loop 具有结构:

    for({loop initialization}; {loop condition}; {per loop process}){
        //the rest of loop process
    }
    

    说明

    • 如您所见,示例 #1 从 1 开始循环(取自 $start = 1)并执行 loop for as long as $start is smaller than 11。为了防止它永远循环,该代码添加了$start++,它转换为$start = $start + 1。这样,对于每个循环,$start 都会加 1。
    • loop condition 必须返回true 才能运行循环。如果返回false,则循环退出。

    现在,让我们检查一下循环内的内容:

    $counter=$counter+1;
    print $counter;
    

    你看那里:$counter=$counter+1。这意味着,您为每个循环将 $counter 加一并打印结果 $counter

    让我们分解这个过程(我们以1 开始循环#,因为它是由$start = 1 定义的):

    loop #   $start  ($start < 11?)  $counter ($counter = $counter + 1)
    1        1       Y               1
    2        2       Y               2
    3        3       Y               3
    4        4       Y               4
    5        5       Y               5
    6        6       Y               6
    7        7       Y               7
    8        8       Y               8
    9        9       Y               9
    10       10      Y               10
    11       11      N               11
    

    从上面的流程分解中,我们可以看到条件 ($start &lt; 11) 是循环 #11 上 false 的结果。这就是为什么结果是1..10,而不是1..11

    示例 #2 也是如此:

    $counter=11;
    $start=1;
    

    循环结构:

    for($start;$start<11;start++) {
        $counter=$counter+1;
    

    让我们分解这个过程:

    loop #   $start  ($start < 11?)  $counter ($counter = $counter + 1)
    1        1       Y               12
    2        2       Y               13
    3        3       Y               14
    4        4       Y               15
    5        5       Y               16
    6        6       Y               17
    7        7       Y               18
    8        8       Y               19
    9        9       Y               20
    10       10      Y               21
    11       11      N               22
    

    这将输出12..21。因为当循环 #11 发生时,它会检查 $start &lt; 11 是否为 false。因此,循环退出。

    【讨论】:

    • 谢谢沉默。你的例子有助于澄清它。本质上是 for 语句中的声明,例如for(循环初始化,循环条件,循环增量)定义循环的长度,例如1..10。非常感谢。更有意义。现在如果我改变了逻辑,如果我错了,请纠正我 $counter=0 $start=1 for($start;$start
    • 不,使用该代码,您最终会执行无限循环。因为,$start true。
    【解决方案5】:

    请分析您的代码.. 您对 $start 和 $counter 变量有明显的混淆。请使用 var_dump 来查看您的变量正在经历什么

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      • 2013-09-24
      • 2018-12-16
      • 1970-01-01
      • 2020-11-13
      • 2018-09-08
      • 2018-05-11
      相关资源
      最近更新 更多