Posted by Jerry Stuckle on 06/26/05 23:18
R. Rajesh Jeba Anbiah wrote:
> Q: How can I output alternate values from a set of values-list?
> A:
>
>
> 2. If you want just to flip-flop between two integers, use XOR logic:
> <?php
> $flip_num = 1;
> $flop_num = 5;
> for($i=0, $n=$flip_num; $i<10; ++$i, $n ^= ($flip_num^$flop_num))
> echo $n;
> ?>
Way more complicated than necessary. Much better:
<?php
$flip_num = 1;
$flop_num = 5;
for($i=0; $i<10; $i++)
echo $i % 2 ? $flip_num : $flop_num;
?>
KISS.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[Back to original message]
|