Control structures in PHP allow you to make decisions, repeat actions, and control the flow of your scripts.
Comparison operators compare two values and return either true or false.
| Operator | Description |
|---|---|
== |
Equal (values only) |
=== |
Identical (value and type) |
!= |
Not equal |
!== |
Not identical |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
<=> |
Spaceship operator (returns -1, 0, or 1) |
<> |
Not equal (alternative to !=) |
<?php
$a = 10;
$b = 20;
var_dump($a < $b); // true
var_dump($a === 10); // true
?>Logical operators combine or invert conditions.
| Operator | Description |
|---|---|
&& |
Logical AND (both conditions must be true) |
| ` | |
! |
Logical NOT (reverses the result) |
and |
Logical AND (lower precedence than &&) |
or |
Logical OR (lower precedence than ` |
xor |
Logical XOR (exactly one condition must be true) |
<?php
$age = 25;
$citizen = true;
if ($age >= 18 && $citizen) {
echo "Eligible to vote";
}
?>Executes code only when a condition is true.
<?php
$a = 20;
$b = 10;
if ($a > $b) {
echo "a is larger than b";
}
?>Executes one block if the condition is true, otherwise another block.
<?php
$a = 5;
$b = 10;
if ($a > $b) {
echo "a is larger than b";
} else {
echo "a is not larger than b";
}
?>Checks multiple conditions in order.
<?php
$marks = 82;
if ($marks >= 90) {
echo "Grade A";
} elseif ($marks >= 75) {
echo "Grade B";
} elseif ($marks >= 50) {
echo "Grade C";
} else {
echo "Fail";
}
?>An if statement inside another if statement.
<?php
$age = 22;
$hasID = true;
if ($age >= 18) {
if ($hasID) {
echo "Entry allowed";
}
}
?>Useful when checking one variable against many possible values.
<?php
$fruit = "banana";
switch ($fruit) {
case "apple":
echo "You chose apple";
break;
case "banana":
echo "You chose banana";
break;
case "orange":
echo "You chose orange";
break;
default:
echo "Unknown fruit";
}
?>-
break;stops execution after a matching case. -
defaultexecutes if no case matches. -
Without
break, execution continues into the next case (fall-through).
match is a modern alternative to switch.
<?php
$status = 200;
$message = match ($status) {
200 => "Success",
404 => "Not Found",
500 => "Server Error",
default => "Unknown Status",
};
echo $message;
?>-
Uses strict comparison (
===). -
Returns a value.
-
No
breakstatements required. -
No accidental fall-through.
-
Prefer
===over==to avoid unexpected type juggling. -
Always use braces
{}even for single-lineifstatements. -
Use meaningful condition names and avoid deeply nested logic.
-
Always include
break;inswitchunless fall-through is intentional. -
Prefer
matchoverswitchin PHP 8+ when returning values. -
Keep conditions simple and readable by extracting complex logic into variables or functions.
- Using
==for authentication or equality checks, which triggers PHP type juggling (e.g."0e12345" == "0e67890"evaluates totrue— the "magic hash" problem) and can lead to authentication bypass. - Forgetting
break;in aswitch, causing unintended fall-through into later cases. - Assigning instead of comparing: writing
if ($x = 5)(assignment, always true) instead ofif ($x === 5). - Relying on loose comparison between strings and numbers, whose semantics changed in PHP 8 and can differ across versions.
- Deeply nested
ifblocks that obscure logic — prefer early returns or guard clauses.
- Type juggling / loose comparison (
==) is a recurring source of vulnerabilities in PHP. Always use strict comparison (===) when validating tokens, passwords, hashes, or any security-sensitive value so that values like"0e123"are not treated as numerically equal. - Compare secrets and hashes with constant-time, type-safe functions such as
hash_equals()andpassword_verify()rather than==/===on raw strings, to resist both type juggling and timing attacks. - Prefer
matchoverswitchfor security decisions: it uses strict comparison and has no fall-through, removing a class of logic-flaw bugs. - Ensure every conditional branch — including the
else/defaultpath — fails closed (denies access) so an unmatched or unexpected input never grants privileges.
- For-Loops — counted iteration
- While-Loops — condition-based iteration
- Foreach-Loops — array iteration
- Switch-Statements — multi-branch selection
- Secure PHP Development — language hub