Reputation: 1126
I needed to increment the for loop by 7 by 7 and used the $x + 7
method as below. However, it didn't work.
for ($x = 1; $x <= 31; $x + 7) {
echo $x ."\n";
}
I solved the problem by redefining the variable as below but I am still wondering why the first method is not working.
for ($x = 1; $x <= 31; $x += 7) {
echo $x ."\n";
}
I usually use the $x++
method to increase the value. Why is $x + 7
different?
You can try: https://glot.io/snippets/g16a4it4il
Upvotes: 0
Views: 302
Reputation: 9273
$x + 7
does not alter x
. It simply evaluates to 7 more than $x
. To add 7 to $x
, you can either:
$x += 7
or
$x = $x + 7
$x++
increments $x
by 1. It is roughly equivalent to $x = $x + 1
or $x += 1
. (Although when used in an expression, $x++
evaluates to the value of $x
before the increment happens; for further information see What's the difference between ++$i and $i++ in PHP?)
Upvotes: 2
Reputation: 731
The for
loop works by changing $x
every iteration until $x <= 31
is no longer true
.
$x + 7
doesn't change $x
, so it'll always remain 1
Upvotes: 1