Akhil Kokani
Akhil Kokani

Reputation: 337

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.

Upvotes: 0

Views: 2055

Answers (6)

Max
Max

Reputation: 387

You start with $i=0, then you do $a+10 and $b+5 as long as $i <5

$i=0, $a=10, $b=5

$i=1, $a=20, $b=10

$i=2, $a=30, $b=15

$i=3, $a=40, $b=20

$i=4, $a=50, $b=25

$i=5, now the loop stops because $i is no longer <5

Upvotes: 2

Hamza Zafeer
Hamza Zafeer

Reputation: 2436

For loop is entry condition loop. It evaluate condition first, so the statement block associated with the loop won't run even once if the condition fails to meet

The statements inside this for loop block will run 5 times, the value of $i will be 0 to 4; Imagine the loops are running separately.

 $a=0;
 $b=0;
 for ($i = 0; $i < 5; $i++){
       echo $a += 10;
       echo '<br>';
 }

And the output like this.

10
20
30
40
50

Now another Loop

for ($i=0; $i < 5; $i++) { 
      echo  $b += 5;
      echo '<br>';

 }

And the output like this

5
10
15
20
25

In each iteration it adds 10 and 5 to previous number of iteration using the assignment operator x += y.

Upvotes: 0

Lee Balino
Lee Balino

Reputation: 490

The for statement is used when you know how many times you want to execute a statement or a block of statements.

enter image description here

Syntax:

for (initialization; condition; increment){
   code to be executed;
}

The initializer is used to set the start value for the counter of the number of loop iterations. A variable may be declared here for this purpose and it is traditional to name it $i.

Example

The following example makes five iterations and changes the assigned value of two variables on each pass of the loop −

<html>
   <body>

      <?php
         $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" );
      ?>

   </body>
</html>

This will produce the following result −

At the end of the loop a = 50 and b = 25

Upvotes: 0

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21661

+= is not increment its an assignment operator.

http://php.net/manual/en/language.operators.assignment.php

As I mentioned in the comments, and others in their answers. += will add the value to on the right to the value on the left. so in a loop its like this

  • step 0 $a = 5 ( 0+5)

  • step 1 $a = 10 (5[previous iteration exit value]+5 )

  • step 2 $a = 15 (10[previous iteration exit value] + 5)

so on....

Of note is you can also do -= *= etc. and .=or append all the same kind of operation.

Upvotes: 0

Vincent Rodomista
Vincent Rodomista

Reputation: 780

This is the way it works.

Let's say you have a no dollars. And I tell you that I will give you a dollar everytime you do 5 chores. However, I will only give you 5 dollars. At first you have no dollars and after one chore I give you 5 dollars. Now you have 5 dollars. You do another chore and I give you another 5, bringing you to ten. I have now given you two dollars. I give you another 5 - you have 15. I give you another - 20 - and one more; bringing you to 25 dollars. Now I have given you my limit of dollars, and our loop is complete.

In this story, my dollars are your $i value. Beginning at 0, working up to 5. Your chores are your $b values, which are added to every time.

Code example:

for ($dollars=0; $dollars < 5; $dollars++) { 
    $chores += 5;
}

Upvotes: 0

O. Jones
O. Jones

Reputation: 108641

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.

Upvotes: 1

Related Questions