Syntax

PHP Foreach Loop Syntax

In PHP, foreach loop: loops over an array with keys and values — foreach ($items as $key => $value) { ... }

Quick Answer

Syntax
foreach ($items as $key => $value) { ... }
PHPSyntax

Foreach Loop

Code example
foreach ($items as $key => $value) { ... }

Loops over an array with keys and values.

Learn More

How to foreach loop in PHP

  1. Open your PHP file or editor.
  2. Copy the example, then replace its sample names and values for your code.

PHP Foreach Loop Syntax

Use foreach ($items as $key => $value) { ... }.

Related Syntax References

Frequently asked questions

What is the PHP syntax for foreach loop?

Use this pattern: foreach ($items as $key => $value) { ... }

Can I copy this PHP code example?

Yes. Use Copy code and adapt the example to your program.

Is this PHP syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify