Michael
Michael

Reputation: 6405

php, loop ( echo from 100 to 100), while

I have a loop within my script and I need to echo "this is another 100" on each 100th time . For example at 147886900, 147886800, 147886700, 147886600 etc

$account_id_new = 147887000;
while($account_id_new > 2)
{
  //do something 
  // echo " this is 100th time";
  $account_id_new = $account_id_new-1;  
}

Upvotes: 1

Views: 532

Answers (3)

John Poulin
John Poulin

Reputation: 71

is the "do something" part of the code actually doing something? Otherwise, you may just want to decrement by 100 instead of by 1.

Otherwise, try the following

$account_id_new = 147887000;
while($account_id_new > 2) {
    if($account_id % 100 == 0)
       echo " this is 100th time";

$account_id_new = $account_id_new-1;

}

Upvotes: 0

TaylorOtwell
TaylorOtwell

Reputation: 7337

Can you use the modulus operator?

if ($account_id_new % 100 === 0)
{
    echo '100th time';
}

Upvotes: 0

You can check if $account_id_new is a multiple of 100 by doing this:

if ($account_id_new % 100 === 0) {
     echo "100 divides the account id evenly.\n";
}

For more information, see the article on the modulo operator on Wikipedia.

Upvotes: 2

Related Questions