Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Understanding PHP for loop

I have a problem understanding how is the result 34 when it should be 32.Because the loop run's 4 times so when u add up 8 to the variable age it should bring up the sum as 32.Maybe I'm wrong please help out to understand.TQ

  <?php
    $age=24;
    for($i=0; $i<=4; $i++){
        $age= $age + 2;
    }
    echo ("At the end of the loop age = $age" );
  ?>
Result >>>>>>At the end of the loop age = 34
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Your loop is not running four times; it's running five times.

$i<4 means the loop will terminate when $i reaches four - it terminates before that execution happens. $i<=4 means "continue looping so long as $i is less than or equal to four"

So, let's work through the examples: for($i=0; $i<=4; $i++)

  • Begins, sets $i to value of 0
  • Loop: is $i less than or equal to 4? $i=0, so yes. $age += 2. ($age now equals 26).
  • End of first loop: $i++ ($i now equals 1).
  • Loop: is $i less than or equal to 4? $i = 1, so yes. $age += 2; ($age now equals 28).
  • End of second loop: $i++ ($i now equals 2).
  • Loop; is $i less than or equal to 4? $i = 2, so yes. $age += 2; ($age now equals 30).
  • End of third loop: $i++ ($i now equals 3).
  • Loop; is $i less than or equal to 4? $i = 3, so yes. $age += 2; ($age now equals 32).
  • End of fourth loop: $i++ ($i now equals 4).
  • Loop; is $i less than or equal to 4? $i = 4, so yes -- $i is equal to four, as specified by <=. $age += 2; ($age now equals 36).
  • End of fifth loop: $i++ ($i now equals 5).
  • Loop; is $i less than or equal to 4? $i = 5, so no. Loop terminates.

Final result: $age = 36

over 4 years ago · Santiago Trujillo Report

0

The number of elements between 0 and positive N is N+1.

  • You loop through 0 1 2 3 4.
  • Those are 5 iterations.
  • 24 + (2*5) = 34.
over 4 years ago · Santiago Trujillo Report

0

Well that's because the loop starts from 0, so it runs 5 times (0,1,2,3,4).

Loop 1 (when value is 0): 24 + 2 = 26;
Loop 2 (when value is 1): 26 + 2 = 28; 
Loop 3 (when value is 2): 28 + 2 = 30; 
Loop 4 (when value is 3): 30 + 2 = 32; 
Loop 5 (when value is 4): 32 + 2 = 34;

you can either start with 1 as:

for($i=1; $i<=4; $i++)

or make it < 4 instead of <= 4, as:

for($i=0; $i<4; $i++)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!