Echo out how many Ones that is generated via simple PHP script |
Echo out how many Ones that is generated via simple PHP script |
sanoj96 |
Jan 14 2018, 06:40 PM
Post
#1
|
Advanced Member Group: Members Posts: 118 Joined: 18-September 12 Member No.: 17,803 |
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 |
Christian J |
Jan 15 2018, 05:11 AM
Post
#2
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
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.) |
sanoj96 |
Jan 15 2018, 11:04 AM
Post
#3
|
Advanced Member Group: Members Posts: 118 Joined: 18-September 12 Member No.: 17,803 |
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.) |
Christian J |
Jan 15 2018, 11:43 AM
Post
#4
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
QUOTE (Long time since i have been working with PHP so i have forgoten allot of it.) Same here. 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"; |
Lo-Fi Version | Time is now: 23rd April 2024 - 04:25 PM |