Advertisement:

Author Topic: While, counting iterations  (Read 330 times)

Offline Study Force

  • SMF Hero
  • ******
  • Posts: 3,107
    • @studyforceps on Twitter
    • Study Force
While, counting iterations
« on: January 11, 2018, 01:48:33 PM »
Hi, general coding question

I have a while statement that ends with count++. $count = 0 is defined outside the while statement.

Is there a way to place the last counted iteration as a variable? For example, if there are 8001 iterations, we set $last_count as 8001.

If so, how can I accomplish this?

Thanks

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 68,361
    • Arantor on GitHub
Re: While, counting iterations
« Reply #1 on: January 11, 2018, 02:33:33 PM »
It'll be in count... the variable doesn't get unset when you leave the loop, at least in PHP.
To assume is to hope that those who came before had the presence of mind and capacity to implement the dreams of those who would come after.

You either die a hero or live long enough to see yourself become the villain. It seems you have chosen which, and now I must do the same.

Offline Study Force

  • SMF Hero
  • ******
  • Posts: 3,107
    • @studyforceps on Twitter
    • Study Force
Re: While, counting iterations
« Reply #2 on: January 11, 2018, 02:37:43 PM »
So echo'ing $count will give me, theoretically, the last count number of 8001?

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 68,361
    • Arantor on GitHub
Re: While, counting iterations
« Reply #3 on: January 11, 2018, 02:38:34 PM »
You could try it and see what happens...
To assume is to hope that those who came before had the presence of mind and capacity to implement the dreams of those who would come after.

You either die a hero or live long enough to see yourself become the villain. It seems you have chosen which, and now I must do the same.

Offline d3vcho(void)

  • On Hiatus
  • SMF Hero
  • *
  • Posts: 2,305
  • Gender: Male
  • Be prepared for hell
    • d3vcho on GitHub
    • @SMF_ES on Twitter
Re: While, counting iterations
« Reply #4 on: January 11, 2018, 02:39:05 PM »
If you do $count = 0 after the loop, then it'll output 0.

Offline Illori

  • Project Manager
  • SMF Master
  • *
  • Posts: 48,371
Re: While, counting iterations
« Reply #5 on: January 11, 2018, 02:47:08 PM »
can you post the code you are using?

Offline Study Force

  • SMF Hero
  • ******
  • Posts: 3,107
    • @studyforceps on Twitter
    • Study Force
Re: While, counting iterations
« Reply #6 on: January 11, 2018, 02:48:01 PM »
Actually, it worked! Thank you.