Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
While-Loops | Loops
Introduction to PHP
course content

Contenido del Curso

Introduction to PHP

Introduction to PHP

1. First Acquaintance
2. Variables and Data Types
3. Conditional Statements
4. Arrays
5. Loops

bookWhile-Loops

A while loop is a programming construct that allows you to execute a specific block of code as long as a certain condition is true.

The syntax for a while loop in PHP is as follows:

  • First, before entering the loop, the condition (which is enclosed in parentheses) is checked. If this condition is true, then the code inside the loop is executed;

  • After executing the code inside the loop, the condition is checked again. If the condition is still true, the loop is executed again. This process continues until the condition becomes false;

  • Once the condition becomes false, the execution of the loop stops, and the program moves on to the code after the loop.

php

main

copy
1234567
<?php $counter = 0; while ($counter < 5) { echo $counter; $counter = $counter + 1; } ?>

The while loop will repeat as long as the value of the variable counter is less than 5. The loop will repeat five times since the variable counter is initialized to 0. Each time the loop repeats, the value of the variable counter will be incremented by 1.

Tarea
test

Swipe to show code editor

Fill in the blanks in the code to create a while loop that calculates the sum of numbers from 1 to 10.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 2
toggle bottom row

bookWhile-Loops

A while loop is a programming construct that allows you to execute a specific block of code as long as a certain condition is true.

The syntax for a while loop in PHP is as follows:

  • First, before entering the loop, the condition (which is enclosed in parentheses) is checked. If this condition is true, then the code inside the loop is executed;

  • After executing the code inside the loop, the condition is checked again. If the condition is still true, the loop is executed again. This process continues until the condition becomes false;

  • Once the condition becomes false, the execution of the loop stops, and the program moves on to the code after the loop.

php

main

copy
1234567
<?php $counter = 0; while ($counter < 5) { echo $counter; $counter = $counter + 1; } ?>

The while loop will repeat as long as the value of the variable counter is less than 5. The loop will repeat five times since the variable counter is initialized to 0. Each time the loop repeats, the value of the variable counter will be incremented by 1.

Tarea
test

Swipe to show code editor

Fill in the blanks in the code to create a while loop that calculates the sum of numbers from 1 to 10.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 2
toggle bottom row

bookWhile-Loops

A while loop is a programming construct that allows you to execute a specific block of code as long as a certain condition is true.

The syntax for a while loop in PHP is as follows:

  • First, before entering the loop, the condition (which is enclosed in parentheses) is checked. If this condition is true, then the code inside the loop is executed;

  • After executing the code inside the loop, the condition is checked again. If the condition is still true, the loop is executed again. This process continues until the condition becomes false;

  • Once the condition becomes false, the execution of the loop stops, and the program moves on to the code after the loop.

php

main

copy
1234567
<?php $counter = 0; while ($counter < 5) { echo $counter; $counter = $counter + 1; } ?>

The while loop will repeat as long as the value of the variable counter is less than 5. The loop will repeat five times since the variable counter is initialized to 0. Each time the loop repeats, the value of the variable counter will be incremented by 1.

Tarea
test

Swipe to show code editor

Fill in the blanks in the code to create a while loop that calculates the sum of numbers from 1 to 10.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

A while loop is a programming construct that allows you to execute a specific block of code as long as a certain condition is true.

The syntax for a while loop in PHP is as follows:

  • First, before entering the loop, the condition (which is enclosed in parentheses) is checked. If this condition is true, then the code inside the loop is executed;

  • After executing the code inside the loop, the condition is checked again. If the condition is still true, the loop is executed again. This process continues until the condition becomes false;

  • Once the condition becomes false, the execution of the loop stops, and the program moves on to the code after the loop.

php

main

copy
1234567
<?php $counter = 0; while ($counter < 5) { echo $counter; $counter = $counter + 1; } ?>

The while loop will repeat as long as the value of the variable counter is less than 5. The loop will repeat five times since the variable counter is initialized to 0. Each time the loop repeats, the value of the variable counter will be incremented by 1.

Tarea
test

Swipe to show code editor

Fill in the blanks in the code to create a while loop that calculates the sum of numbers from 1 to 10.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 5. Capítulo 2
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt