Akhil Kokani Akhil Kokani - 1 year ago 75
PHP Question

How does for loop works actually in php?

I'm a newbie to php and I've understood all other loops in php but the question is I cannot understand how the for loops works for ex:
Here is the code;

$a = 0;
$b = 0;

for ($i=0; $i < 5; $i++) {
$a += 10;
$b += 5;
echo("At the end of the loop a=$a and b=$b");

When I execute this script the value of a = 50 and b = 25!

Is it multiplying the a value with i's increment value? like 10 * 5 = 50.

Answer Source

Your loop runs five times. Each time through the loop you add 10 to the value of $a. Doing that five times gives you 50.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download