Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Server-side Scripting _ Echo out how many Ones that is generated via simple PHP script

Posted by: sanoj96 Jan 14 2018, 06:40 PM

Hello,


I have made a race that finds all numbers that 7 can be divided up to 1000. The task is to find out how many of these numbers exist, that is the number. Thus I put all the values ‚Äč‚Äčinto a common number that was 1.

What I'm wondering is how I can show, with simple php coding, how many 1s are being generated, ie something with count or something

Here is the PHP Code i have writen!


CODE

    <?php
  
    for ($number=1; $number<=1000; $number++)
    { if ($number % 7 == 0) {
        //print"$number <br>";}
        $antall = $number%$number == 0;
      
    }
     $enere = array("1");
    echo count ($enere);
    }
  
    ?>


Thanks in advanced!
Sanoj96

Posted by: Christian J Jan 15 2018, 05:11 AM

You mean you want to find all integers that can be divided evenly with 7 (I don't remember much of modulus)? Create an empty array before the loop, then populate the array with items inside the IF statement. After the loop you can get the number of array items with count().

(BTW I might generally start such counters with $number=0, just for good practice, but in this case I guess $number=7 is lowest possible integer.)

Posted by: sanoj96 Jan 15 2018, 11:04 AM

The script is posting lots of "1" to the website. is there a way to count em? Like if it post 15 "1"s it should be a way that the site says "There is 15 1s generated."

I have tried a few things out now and i cant get it working.

(Long time since i have been working with PHP so i have forgoten allot of it.)

Posted by: Christian J Jan 15 2018, 11:43 AM

QUOTE
(Long time since i have been working with PHP so i have forgoten allot of it.)

Same here. tongue.gif

If you add an item to the array instead, you can count the array items after the loop has finished with count().

Or you could increment the value of a counter variable, something like:

CODE
$matches=0;
for(...)
{
    if(...)
    {
        $matches++;
    }
}
echo "There are $matches 1s generated";

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)