odd even number

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
theclay7
Forum Commoner
Posts: 50
Joined: Wed Feb 19, 2003 3:17 am

odd even number

Post by theclay7 »

if I have a for loop :
for ($i=0; $i<count($replyDetails); $i++){
...
bgcolor = 123 (OR 456)...
...
}
and will show bgcolor = 123 if $i is an even number or $i=0
and will show bgcolor = 456 if $i is an odd number

how can i implement that?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can use the modulus operator:
http://www.php.net/manual/en/language.o ... hmetic.php

Code: Select all

<?php
for ($i=1; $i < 11; $i++) {
	echo '<p>'.$i.': ';
	echo ($i%2) ? 'odd' : 'even';
	echo '</p>';
}
?>
Mac
Post Reply