Re: help with script needed

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

The order matters, though, as moving the 15 "after" the other if tests
would fail, as 3 and/or 5 would kick in first.

It's also a "game" played in grade school when learning "clock
arithmetic" (aka the modulus operator) at least here in the Midwest,
usually around 4th grade (age 10), as I recall.

Each person in turn says the next number or "Fizz" or "Buzz" or
"FizzZBuzz", and if somebody messes up, you razz them for it, and
start over at 1.

The person who makes the mistake could also be "out" in a musical
chairs sort of thing, though that risks boredom creeping in for a
large group of people no longer actively playing.  You do not want a
roomful of bored 4th-graders.  Trust me on that one. :-)

If the numbers are not mutually prime, you would have to do an && test
as Stut posted.

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.

YMMV

On Wed, March 7, 2007 4:00 pm, Bruce Gilbert wrote:
> Thanks for the responses so far.
>
> This is what I have come up with
> [php]
> <?php
> for( $i=1; $i<=100; $i++ )
> {
> echo $i;
> echo "<br>";
> if ($i%3 == 0) echo "Foo ";
> elseif ($i%5 == 0) echo "Bar";
> }
>
> ?>
> [/php]
>
> and the results can be seen here
> http://www.inspired-evolution.com/image_test/numbers_output.php
>
> I actually want the Foo and Bar to replace the numbers they are
> dereivatives of and not appear below as they are now, and I also need
> to add a third condition for "FooBar" which would be for numbers that
> are both divisible by 3 and 5.
>
> thanks
>
> On 3/7/07, Martin Marques <martin@xxxxxxxxxxxxxxx> wrote:
>> Tijnema ! escribió:
>> > On 3/7/07, Bruce Gilbert <webguync@xxxxxxxxx> wrote:
>> >> I just need to add code to print something different, say "foo"
>> if the
>> >> output is a multiple of 5 or 10 for example. How do I go about
>> doing
>> >> this?
>> >>
>> >
>> > I've seen that question a lot, what i use is fairly simple
>> > if( intval($number / $multiple) == ($number / $multiple ) )
>> >
>> > try that
>>
>> Very bad solution. Try the % operator:
>>
>> if($number % $multiple == 0)
>>
>> http://www.php.net/manual/en/language.operators.arithmetic.php
>>
>> --
>> select 'mmarques' || '@' || 'unl.edu.ar' AS email;
>> ---------------------------------------------------------
>> Martín Marqués          |   Programador, DBA
>> Centro de Telemática    |     Administrador
>>                 Universidad Nacional
>>                      del Litoral
>> ---------------------------------------------------------
>>
>
>
> --
> ::Bruce::
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux