|
Posted by Jerry Stuckle on 06/27/05 14:28
R. Rajesh Jeba Anbiah wrote:
> Jerry Stuckle wrote:
>
>>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.
>
>
> Not really. Your logic requires an iterator variable $i; but none of
> the above logic requires such. So, the above logics can easily be
> incorporated while reading DB or using foreach or while loops.
>
> --
> <?php echo 'Just another PHP saint'; ?>
> Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com
>
Rajesh,
Your code also requires and iterator variable $i; additionally it
requires a second variable $n and uses a double xor logic.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|