Johnny000
Johnny000

Reputation: 2104

While Loop till number reached

I want to do something like this

$x = 630;
$y = 10;

while ($y < $x){
// do something
$y+10;
}

When I use $y++ it's working and adding +1, but with +10 it's not working. But I need to go in +10 steps. Any pointers ?

Upvotes: 1

Views: 305

Answers (3)

gurudeb
gurudeb

Reputation: 1876

// commenting the code with description
$x = 630; // initialize x
$y = 10;  // initialize y

while ($y < $x){ // checking whether x is greater than y or not. if it is greater enter loop
// do something
$y = $y+10; // you need to assign the addition operation to a variable. now y is added to 10 and the result is assigned to y. please note that $y++ is equivalent to $y = $y + 1
}

Upvotes: 0

Duane
Duane

Reputation: 4499

This is because $y++ is equivalent to $y = $y + 1; You are not assigning the new value in $y. Please try

$y += 10;

OR

$y = $y + 10;

Upvotes: 1

Alain
Alain

Reputation: 36954

in your code, you are not incrementing $y : $y+10 returns the value of $y plus 10, but you need to assign it to $y.

You can do it with several ways :

  • $y = $y + 10;
  • $y += 10;

Example :

$x = 630;
$y = 10;
while ($y < $x){
    // do something
    $y = $y + 10;
}

Upvotes: 2

Related Questions