You foreach ($myArray as $k => $v) loop is executed 3 times as $myArray has 3 Elements.
First run
$k = 0 which is the index of the first element, $v = "a"
echo $v; // Outputs a
Output of your loop
for ($i = 1; $i < 5; $i++) { ... }
Outputs all numbers from 1 to 5 and stops once the exact value of $k is met. $k is 0 so the condition (break) never gets triggered. Hence all numbers from 1 to 5 are echoed.
Output so far:
a1234
Second run
$k = 1 which is the index of the second element, $v = "b"
for ($i = 1; $i < 5; $i++) { ... }
Output of your loop
for ($i = 1; $i < 5; $i++)
Outputs all numbers from 1 to 5 and stops once the exact value of $k is met. As $k is 1 the break gets executed on first run of llop, hence no output.
Output so far:
a1234b
Third run
$k = 2 which is the index of the third element, $v = "c"
echo $v; // Outputs c
Output of your loop
for ($i = 1; $i < 5; $i++) { ... }
Outputs all numbers from 1 to 5 and stops once the exact value of $k is met. As $k euqals 2 this time the loop gets executed once outputting 1. On second run the break executes and terminates oputput
Final output:
a1234bc1