For Each Loop
Parameter | Purpose | Example |
---|---|---|
$list | The list to iterate through | $users |
$alias | The variable to store the item currently being iterated through | user |
Example
Example

For Loop
Parameter | Purpose | Example |
---|---|---|
$loops | The number of iterations in the loop | 10 |
$index | The name of the index variable | index |
Example
Example

While Loop
Parameter | Purpose | Example |
---|---|---|
conditions | The condition(s) that the loop will use to determine if it continues to run | false == false && true == true |
Example
Example

Break Out Of Loop
Example
Example

Continue to Next Iteration
Example
Example

Iterating Over an Array of Objects
You can access properties of each item in aforeach
loop:Nested Loops
You can nest loops for multi-dimensional data:When to Use For vs Foreach
- Use
for
when you need a fixed number of iterations or an index variable. - Use
foreach
to iterate over each item in a list or array.
Notes on Break and Continue
Thebreak
and continue
statements work in all loop types (for
, foreach
, and while
). Use break
to exit the loop early, and continue
to skip to the next iteration.