简体   繁体   English

如何在foreach / for循环中每次添加超过1个?

[英]How to add more than 1 each time in a foreach/for loop?

How do you add more than 1 each time in a foreach/for loop? 如何在foreach / for循环中每次添加超过1个?

I know the basic +1 is like this: 我知道基本的+1是这样的:

for ($i=1;$i<$nc;$i++){

 echo $i;

}

but I need to add another element which goes up by 5 each time also 但我需要添加另一个元素,每次也增加5

    for ($i=1;$i<$nc;$i++){

       // what ever maths to make $plus5 go up by 5 each time

       echo $i . ' - ' . $plus5;

}

so the result would be: 结果将是:

1 - 5
2 - 10
3 - 15
4 - 20
for ($i=1,$j=5;$i<$nc;$i++,$j=$i*5){
 echo $i.' - '.$j;
}
for ($i=1; $i<$nc; $i++) {
    echo $i . '-' . $i*5;
}
for($i=1;$i<$nc;$i++)
{
    echo $i.' - '.$i*5;
}
 for ($i=1;$i<$nc;$i++){

       $b = $i*5;

       echo $i . ' - ' . $b . '<br/>';           

}
for ($i=1,$plus5=5;$i<$nc;$i++,$plus5+=5){
   echo $i . ' - ' . $plus5 . "\n";
}
 for ($i=1;$i<$nc;$i++){
    $plus5 = $i*5;
    echo $i." - ".$plus5."\n";
 }

if only you want the iteration times 5 use $i * 5 as other mentiened 如果你只想要迭代次数5使用$i * 5作为其他精神

and you can use like this : 你可以像这样使用:

for($i=0, $j=0 ; $i<10 ; $i++,$j = $j+5 ){
    echo $i . "<br/>";
    echo $j. "<br/>";
}

This might be useful: 这可能很有用:

<?php
for ($i=1;$i<nc;$i++){

     $b = $i*5;
     echo $i . ' - ' . $b;     
     echo '<br/>';
}
?>"

Simply try: 只需尝试:

for ($i=1;$i<$nc;$i++){
   // what ever maths to make $plus5 go up by 5 each time
   echo $i*5;

} that's it.then it outputs 5,10,15,20..... 那就是它。然后输出5,10,15,20 .....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM