Skip to main content

PHP Loops

Loops allow us to execute a block of code multiple times.

Without loops, programmers would need to write the same code repeatedly.

Loops are one of the most important concepts in programming.


Why Loops Matter

Loops are used everywhere:

✅ Displaying online players

✅ Reading database records

✅ Processing APIs

✅ Generating scoreboards

✅ Handling inventories

✅ Iterating configurations

✅ Network packet handling


Real World Example

Suppose there are 100 players online.

Without loops:

echo $players[0];
echo $players[1];
echo $players[2];

This is impossible to maintain.

Instead:

foreach ($players as $player) {
echo $player;
}

Types of Loops

PHP provides:

  1. for
  2. while
  3. do-while
  4. foreach

The for Loop

Used when the number of iterations is known.

Syntax:

for (
initialization;
condition;
increment
) {

}

Example

for (
$i = 1;
$i <= 5;
$i++
) {
echo $i;
}

Output:

12345

Execution Flow

Initialize

Check Condition

Execute Code

Increment

Repeat

Example With Text

for (
$i = 1;
$i <= 3;
$i++
) {

echo "Hello";
}

Output:

Hello
Hello
Hello

Decrement Loop

for (
$i = 10;
$i >= 1;
$i--
) {

echo $i;
}

Output:

10987654321

Infinite Loop

⚠ Dangerous.

for (;;) {

}

Never do this unless necessary.


Multiplication Table

for (
$i = 1;
$i <= 10;
$i++
) {

echo 5 * $i;
}

while Loop

Used when the number of iterations is unknown.

Syntax:

while (condition) {

}

Example

$i = 1;

while ($i <= 5) {

echo $i;

$i++;
}

Output:

12345

Execution Flow

Check Condition

Execute Code

Update Variables

Repeat

Example

$health = 20;

while ($health > 0) {

echo $health;

$health--;
}

Infinite While Loop

while (true) {

}

Common in:

  • Servers
  • Network Applications
  • Game Loops

do-while Loop

Executes code at least once.

Syntax:

do {

}
while(condition);

Example

$i = 1;

do {

echo $i;

$i++;

}
while ($i <= 5);

Difference Between While and Do While


While

Checks first.

Condition

Execute

Do While

Executes first.

Execute

Condition

Example

$i = 10;

do {

echo "Hello";

}
while ($i < 5);

Output:

Hello

Executed once.


foreach Loop

Most important loop in PHP.

Used to iterate arrays.


Example

$players = [
"Steve",
"Alex",
"Aayan"
];

foreach (
$players as $player
) {

echo $player;
}

Output:

Steve
Alex
Aayan

Associative Arrays

$user = [
"name" => "Aayan",
"age" => 18
];

foreach (
$user as $key => $value
) {

echo $key;
echo $value;
}

Output:

name Aayan
age 18

By Reference

foreach (
$players as &$player
) {

$player =
strtoupper($player);
}

⚠ Always unset afterwards.

unset($player);

Nested Loops

Loops can exist inside loops.

Example:

for ($i = 1; $i <= 3; $i++) {

for ($j = 1; $j <= 3; $j++) {

echo "$i:$j";

}

}

Break Statement

Stops loop execution.

Example:

for ($i = 1; $i <= 10; $i++) {

if ($i === 5) {
break;
}

echo $i;
}

Output:

1234

Continue Statement

Skips current iteration.

for ($i = 1; $i <= 5; $i++) {

if ($i === 3) {
continue;
}

echo $i;
}

Output:

1245

Break Levels

break 2;

Exits multiple nested loops.


Continue Levels

continue 2;

PocketMine Examples


Online Players

foreach (
$this->getServer()
->getOnlinePlayers()
as $player
) {

$player->sendMessage(
"Welcome!"
);
}

Config Values

foreach (
$config->getAll()
as $key => $value
) {

}

Scoreboard Updates

foreach (
$players as $player
) {

$this->updateScoreboard(
$player
);
}

Saving Data

foreach (
$this->playerData
as $name => $data
) {

$this->save($name);

}

Performance Notes


Fastest Loops

Generally:

for
foreach
while

The difference is usually tiny.

Always prioritize readability.


Common Mistakes


Forgetting Increment

$i = 1;

while ($i <= 5) {

}

Infinite loop.


Modifying Array During foreach

Can lead to unexpected behavior.


Missing Break

switch (...)

Best Practices

✅ Use:

foreach

for arrays.


✅ Use:

for

when iteration count is known.


✅ Avoid unnecessary nested loops.


Real Project Example

Broadcast message:

foreach (
$this->getServer()
->getOnlinePlayers()
as $player
) {

$player->sendMessage(
"Server Restarting"
);

}

Exercises


Exercise 1

Print numbers:

1-10

using for loop.


Exercise 2

Print:

10-1

Exercise 3

Create array:

$players = [
"Steve",
"Alex",
"Aayan"
];

Display using foreach.


Exercise 4

Skip number:

5

using continue.


Challenge

Create multiplication table:

1 x 1 = 1
1 x 2 = 2

up to:

10 x 10

using nested loops.


Quiz

Which loop is best for arrays?

Answer

foreach


Which statement skips iteration?

Answer

continue


Which statement exits loop?

Answer

break


Useful Links

PHP Loops:

https://www.php.net/manual/en/control-structures.for.php

While:

https://www.php.net/manual/en/control-structures.while.php

Foreach:

https://www.php.net/manual/en/control-structures.foreach.php


Next Chapter

➡ PHP Functions