14 replies
Ok well I am tring to do is count to 100 but the catch is what I want to do is every number of 3 like 3 6 9 12 ect.. needs to say fizz and all the numbers 5 10 ect.. needs to say buzz and all the numbers 15 30 45 ect.. needs to say fizzbuzz I have tried to do it but i am stumped I can sloopy code it but i want it so if i want it to do it up to 500 or a 1000 i can not just 100 cause thats sloopy I really have not delt with counting in php I have a prgrep script that will replace but i want to set up so it counts by 2 4 6 8 then i can fig it out but this has got me stumped

thank you
#counting #php
  • Profile picture of the author jamesc1985
    lol are you on codeacademy?
    {{ DiscussionBoard.errors[7621905].message }}
    • Profile picture of the author jiffyspop
      no I am just tring to do it for myself code quize i gave myself its driving me boonkers that i can not fig out
      {{ DiscussionBoard.errors[7621969].message }}
  • Profile picture of the author David V
    This page on php.net might help you.
    {{ DiscussionBoard.errors[7621985].message }}
  • Profile picture of the author Dan Grossman
    There are lots of ways to write this, but here's one:



    WF is stripping out half the code so I took a screenshot and inserted that.

    % is the modulus operator and returns the remainder of integer division.

    A multiple of 3 will have a remainder of 0 when divided by 3.

    A multiple of 5 will have a remainder of 0 when divided by 5.

    By checking both those conditions in series, you'll print "Fizz" then "Buzz" when the number is a multiple of both 3 and 5.
    Signature
    Improvely: Built to track, test and optimize your marketing.

    {{ DiscussionBoard.errors[7622221].message }}
    • Profile picture of the author jiffyspop
      wow thank you so much I suck at doing math in php would like to make beeter in my skills know i got to fig the fizzbuzz lol :confused:
      {{ DiscussionBoard.errors[7622424].message }}
    • Profile picture of the author jiffyspop
      You guys are awsome thank you so much I was going boonnkers with it and all I have say awsome

      thank you
      {{ DiscussionBoard.errors[7622456].message }}
  • Profile picture of the author festi9
    for($i = 0 ; $i < $num ; $i++)
    {
    if($i%3 == 0 && $i%5== 0)
    echo "fyzzbyzz";
    else if($i%3 == 0)
    echo "fyzz";
    else if($i%5 == 0)
    echo "byzz";
    }

    Hope the above code will help you understand
    {{ DiscussionBoard.errors[7623875].message }}
  • Profile picture of the author jiffyspop
    Thank you very much You got it right the first time thank you for the code I totaly got it I just haven't been coding for 6 months or so being rusty and tring to get back into it the first code i wrote for this was a pregrep and replace but was having a prob with numbers on it. I was wondering when you a pregrep replace with a number do you need to do something like this '/3/' or is it '{3}' just never had to do a number in a pregrep.
    {{ DiscussionBoard.errors[7626636].message }}
  • Profile picture of the author Dan Grossman
    Do you mean preg_replace, which means PCRE (perl-compatible regular expression) regular expression replace? Pregrep isn't a word or nickname for anything.

    If you want to match the number 3, the pattern is just 3. No slashes, no curly braces. {3} would tell the regular expression matcher to repeat the previous pattern 3 times. The whole pattern, though, needs to be surrounded by a delimiter, which can be any character you want. The forward slashes are commonly chosen.

    Regular expressions are a complex topic all of their own.

    If you just want to replace a literal string with a different literal string (i.e. change all the "3"s to something else), you don't need a regular expression. Use str_replace.



    Signature
    Improvely: Built to track, test and optimize your marketing.

    {{ DiscussionBoard.errors[7626837].message }}

Trending Topics