Operators, Conditionals and Loops
Comparison: == vs ===
0 == "0" // true (converts types first)n0 === "0" // false (checks type too -- prefer this)n"5" == 5 // truen"5" === 5 // false
Exactly like JavaScript, always prefer === and !== over == and != in PHP. PHP’s automatic type conversion in loose comparisons has historically caused genuine, security-relevant bugs — including cases where certain strings unexpectedly compared equal to 0 — so strict comparison is now considered standard best practice throughout the PHP community.
if / elseif / else
$score = 82;nif ($score >= 90) {n $grade = "A";n} elseif ($score >= 75) {n $grade = "B";n} else {n $grade = "C";n}
Note PHP’s keyword is elseif written as one word, not else if as two — both actually work in PHP, but elseif is the more idiomatic, commonly seen style.
Loops
for ($i = 0; $i < 5; $i++) {n echo "Lesson $in";n}nnforeach (["Python", "PHP", "SQL"] as $course) {n echo $course . "n";n}nnforeach (["name" => "Yash", "course" => "PHP"] as $key => $value) {n echo "$key: $valuen";n}
foreach is by far the most common loop in everyday PHP code — it works over both plain lists and key-value arrays, with the second form (as $key => $value) letting you access both the key and the value together on each pass through the loop.
The null coalescing operator
$name = $_GET["name"] ?? "Guest";
?? returns the left-hand value if it exists and is not null, otherwise it returns the right-hand fallback. This is used constantly in PHP for safely reading values that might not be set — like data submitted through a web form, covered in an upcoming lesson.