Richard Lynch wrote:
You could also just use:
if (($i % 15) === 0) echo "FooBar";
elseif (($i % 3) === 0) echo "Foo";
elseif (($i % 5) === 0) echo "Bar";

15 works because 3 and 5 are mutually prime or whatever it's called.

Good point, missed that one.

A minimalist might not even bother with the 15 and would skip the
'else' and just do:
if (($i % 3) === 0) echo "Foo";
if (($i % 5) === 0) echo "Bar";
echo "\n";

I personally think that's less comprehensible, however, and less
maintainable, so I'd rather see a couple more CPU cycles than have
code I have to think about to figure out what it does, until it's a
proven bottleneck.

Except that you're not displaying the actual number when it's not divisible between 3 or 5, so that doesn't meet the spec. Sorry.

-Stut

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to