【问题标题】:Adding or condition in for loop在 for 循环中添加或条件
【发布时间】:2013-08-10 19:53:08
【问题描述】:

如何在for loop 支票中添加or? 例如:

for ($i=1; ($i <= $untilPoint) or ($i <= $points); $i++){
.. code ...
}

【问题讨论】:

  • 如果它不起作用(是吗?),那么你可以把它变成一个do/while 循环。

标签: php loops for-loop


【解决方案1】:

您输入的正是有效的 PHP。比如这个程序:

<?
$untilPoint = 3;
$points = 5;
for ($i=1; ($i <= $untilPoint) or ($i <= $points); $i++){
    echo("$i\n");
}
?>

打印这个:

1
2
3
4
5

(在 PHP 5.2.17 中测试,从 Bash 提示符运行)。

也就是说,我想知道您是否真的需要and 而不是or?如果$i 是点数组的索引,其中$points 是点数,$untilPoint 是任意截止值,那么您需要and,而不是or

【讨论】:

    【解决方案2】:

    应该是这样的,可能运行得稍微快一些。 您可以用 分隔,更多信息请参阅http://php.net/manual/en/control-structures.for.php

    <?
    $untilPoint = 3;
    $points = 5;
    for ($i=1; $i <= $untilPoint, $i <= $points; $i++){
        echo($i , "\n");
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-02
      • 2020-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 2021-02-18
      • 2011-10-10
      相关资源
      最近更新 更多