douguxun6866 2014-10-17 04:01
浏览 170
已采纳

PHP循环每十分钟

How to make loop per tens like this one,

<?php

for ($i=0; $i < 30; $i++) { 
    if ($i == 1 || $ == 2 ... $i == 10) {
        # code...
    } elseif ($i == 11 || $ == 12 ... $i == 20) {
        # code...
    } else {
        # code...
    }

}

I need 1 2 3 4 5 is different from 11 12 13 14 15 and 21 22 23 24 25

  • 写回答

2条回答 默认 最新

  • dongqiao8417 2014-10-17 04:09
    关注

    There are several ways you could approach this :

    1. Separate them

    Since clearly, you're doing different code to the ranges 1-10, 11-20, a,d 21-30, then it would do you good to separate them into several for loops.

    for ($i=1; $i <= 10; $i++) { 
        //code for $i 1-10
    }
    
    for ($i=11; $i <= 20; $i++) { 
        //code for $i 11-20
    }
    
    for ($i=21; $i <= 30; $i++) { 
        //code for $i 21-30
    }
    

    2. Put the conditionals inside one for loop, but use <= instead of ==

    for ($i=1; $i <= 30; $i++) { 
        if($i <= 10){
            //code for $i 1-10
        }
        else if($i <= 20){
           // code for $i 11-20
        }
        else{
           // code for $i 21-30
        }
    }
    

    Alternatively, you could use $i > 0 && $i <= 10 for the if conditions if you prefer or for readability, but the above code does exactly the same with less.

    Personally, for your specific example, I would prefer using the first option, as it is much more readable (for me).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?