W3docs

endforeach

The "endforeach" keyword is a control structure in PHP that is used to mark the end of a "foreach" loop. In this article, we will explore the syntax and usage

The PHP endforeach Keyword

endforeach is the closing keyword for PHP's alternative syntax for the foreach loop. Instead of wrapping the loop body in curly braces, you open the block with a colon (:) and close it with endforeach;. The behavior is identical to the brace version — only the punctuation changes.

This page covers the syntax, when to use it, working examples, and the gotchas (mixing syntaxes, references) that trip people up.

Syntax

You replace the opening { with : and the closing } with endforeach;:

foreach ($array as $value):
  // code to be executed
endforeach;

It also works with the key/value form:

foreach ($array as $key => $value):
  // code to be executed
endforeach;

Both are functionally identical to the standard brace syntax:

foreach ($array as $value) {
  // code to be executed
}

When to use it

The alternative syntax exists mainly for templates — files that mix PHP and HTML. A closing } can be hard to match up with its opening brace when there is HTML in between, whereas endforeach; is self-documenting:

<ul>
<?php foreach ($products as $product): ?>
  <li><?= htmlspecialchars($product) ?></li>
<?php endforeach; ?>
</ul>

In plain PHP code (no embedded HTML), most teams prefer the brace syntax. Pick one style per file and stay consistent.

Examples

Examples of PHP endforeach

<?php

// Example 1
$array = ["apple", "banana", "cherry"];
foreach ($array as $value):
  echo $value . PHP_EOL;
endforeach;

// Output:
// apple
// banana
// cherry

// Example 2
$array = ["a" => "apple", "b" => "banana", "c" => "cherry"];
foreach ($array as $key => $value):
  echo $key . " = " . $value . PHP_EOL;
endforeach;

// Output:
// a = apple
// b = banana
// c = cherry

Example 1 iterates over a plain list and prints each value; Example 2 iterates over an associative array, capturing both the $key and the $value.

Common mistakes

Don't mix the two syntaxes. If you open with :, you must close with endforeach; — and if you open with {, you must close with }. Mixing them is a parse error:

// Parse error: this is invalid
foreach ($array as $value): // opens with a colon
  echo $value;
} // ...but tries to close with a brace

Remember the semicolon. It is endforeach;, not endforeach. Forgetting the semicolon is a common typo that triggers a syntax error.

The reference gotcha applies here too. As with any foreach, if you loop by reference (as &$value) the $value variable still points at the last element after the loop ends. Always unset($value) afterward to avoid surprising bugs:

$array = [1, 2, 3];
foreach ($array as &$value):
  $value *= 2;
endforeach;
unset($value); // break the reference

print_r($array);
// Output:
// Array ( [0] => 2 [1] => 4 [2] => 6 )

endforeach is one of a family of alternative-syntax terminators. Each loop and conditional has its own:

For the loop itself, see the full foreach loop chapter.

Conclusion

endforeach closes the colon form of a foreach loop. Reach for it in templates where PHP and HTML are interleaved, keep the syntax consistent within a block, and remember the trailing semicolon.

Practice

Practice
What is the syntax to use the endforeach control structure in PHP?
What is the syntax to use the endforeach control structure in PHP?
Was this page helpful?