Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen While-Loops | Schleifen
Einführung in PHP

While-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:

while (condition) { 
// Statements to be executed while a condition is true 
}
  • 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.

93ea0039-73f2-4783-a8e8-b2f71cf48aa0 img2
Note
Study More

It is important to remember that while loops can be infinite if the condition never becomes false.
Always check the condition carefully to avoid infinite loops.

main.php

main.php

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.

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 5. Kapitel 2
single

single

While-Loops

Swipe um das Menü anzuzeigen

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:

while (condition) { 
// Statements to be executed while a condition is true 
}
  • 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.

93ea0039-73f2-4783-a8e8-b2f71cf48aa0 img2
Note
Study More

It is important to remember that while loops can be infinite if the condition never becomes false.
Always check the condition carefully to avoid infinite loops.

main.php

main.php

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.

Aufgabe

Wischen, um mit dem Codieren zu beginnen

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

  1. The variable $sum is initialized with 0 to store the total sum.
  2. The variable $number is initialized with 1 to start counting from 1.
  3. Complete the while condition so that the loop continues as long as $number is less than or equal to 10.
  4. Inside the loop, add the current value of $number to $sum using the += operator.
  5. Increment $number by 1 in each iteration to move to the next number.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 5. Kapitel 2
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt