PHP if is a multiple of an integer

后端 未结 5 1978
遥遥无期
遥遥无期 2021-01-25 08:55

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 example, what i could do is:

for($i=0;          


        
5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-25 09:34

    Use the modulus operator.

    if (! (($i+1) % 3) ) {
    

    If $i+1 divides into 3 with no remainder, the result will be zero. Then you just need a boolean not.

    If you want to match 0 as well (since you use it in your example, but it doesn't match your description) then you will have to special case it with an ||.

提交回复
热议问题