PHP,如果是整数的倍数 [英] PHP if is a multiple of an integer

查看:209
本文介绍了PHP,如果是整数的倍数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在for循环中,我需要添加一些仅在循环位于[(3的倍数)减1]时输出的HTML.

Within a for loop, i need to add some HTML that outputs only when the loop is on a [(multiple of 3) minus 1].

例如,我能做的是:

for($i=0; $i<count($imagearray); $i++)
{
    if($i=="0" || $i=="2" || $i=="5" || $i=="8" || $i=="11")
    {
        echo 'string';
    }
}

但是对于大的for循环来说,这不是很优雅,也没有用,有没有合适的方法来做到这一点?

but this isnt very elegant and extremely useless for big for loops, is there a proper way to do this?

推荐答案

if ( $i==0 || ($i+1)%3 == 0 )
{
    //do stuff
}

这将执行的操作是转到下一个索引,将其除以3,然后查看是否还有余数.如果没有,则表示当前索引比可被3整除的数字小一.

What this will do, is go to the next index, divide it by 3, and see if there is a remainder. If there is none, then that means that the current index is one less than a number that is divisible by 3

这篇关于PHP,如果是整数的倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆