Module 2 – PHP Fundamentals
Lesson
IF Statements
Program branching
if, elseif, and else enable different execution paths. The condition must be an expression that evaluates to bool. For short checks, use the ternary operator ?:. A guard clause – early return – often makes code more readable than deeply nested if statements.
Validation example
Check whether the email is empty before submitting the form; display an error message next to the field.
<?php
$godine = 17;
if ($godine >= 18) {
echo "Punoletan";
} else {
echo "Maloletan";
}Branching in practice
Model a user age check for content access (e.g. 18+). Add a branch for invalid input.
<?php
$godine = (int) ($_POST["godine"] ?? 0);
if ($godine >= 18) {
echo "Pristup dozvoljen";
} elseif ($godine > 0) {
echo "Morate imati 18+ godina";
} else {
echo "Unesite validan broj";
} 