Module 2 – PHP Fundamentals
Lesson
Functions
Reusing logic
function ime($parametar): tip { return ... } defines a block you call multiple times. Type hints and return type increase reliability. Anonymous functions and arrow fn ($x) => $x * 2 are used in array_map and callbacks.
Scope
Access global variables inside a function via global $x or, better, pass them as arguments.
<?php
function formatCena(int $din): string {
return number_format($din, 0, ",", ".") . " RSD";
}
echo formatCena(1500);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.
