Module 2 – PHP Fundamentals
Lesson
Foreach
Traversing arrays
foreach ($niz as $vrednost) and foreach ($niz as $kljuc => $vrednost) are standard for user lists, products, and associative configuration maps. Reference &$v allows modifying array elements during traversal.
Associative array
Keys can be strings (e.g. "email" => "a@b.rs") – typical form of a single database row before an object.
<?php
$korisnik = ["ime" => "Marko", "grad" => "Beograd"];
foreach ($korisnik as $polje => $v) {
echo htmlspecialchars($polje) . ": " . htmlspecialchars($v) . "<br>";
}Example variation
Extend the lesson example: add another variable, change the loop boundary or branching, and observe the output. Deliberately introduce an error (e.g. an undefined variable) to see the PHP message – then fix it.
