Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda