简体   繁体   中英

Print something every 10 loop iterations

I need to print something in a loop everytime a number is 10, 20, 30, 40...

It should only print once and then continue the loop and print again when the number is divisible by 10.

How can I achieve this?

for ($i = 0; $i < 100; $i++) {
    if ($i % 10 == 0) echo $i."\n";
    // do something
}

Generally the modulus operator (%) is what you need here. It calculates the remainder of a division. So: 7 % 5 = 2 and 18 % 5 = 3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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