PHP if Statement
In this page:
if (condition) {
// runs only when condition is true
}
Basic if Statement
An if statement runs its block only when the condition inside the parentheses evaluates to true; if it's false, PHP skips the entire block and moves straight to whatever comes after it — no error, no output, just silently continuing execution.
Example: Basic if Statement
<?php
// Declare `$age`, set to `20`
$age = 20;
// Check whether `$age >= 18`
if ($age >= 18) {
// Print "You are an adult." to the output
echo "You are an adult.";
}
?>
Login to try C/C++/Java/PHP code in the editor
Comparison inside Conditions
=== compares both value and type without any automatic conversion, which makes it the safer default inside an if condition — if ($status === active) won't accidentally match 1 or "1" the way the looser == operator sometimes can.
Example: Comparison inside Conditions
<?php
// Declare `$status`, set to "active"
$status = "active";
// Check whether `$status === 'active'`
if ($status === 'active') {
// Print "Status is active." to the output
echo "Status is active.";
}
?>
Login to try C/C++/Java/PHP code in the editor
Logical AND (&&) in Conditions
&& inside a condition requires every joined expression to be true before the block runs — if ($age >= 18 && $hasConsent) only executes when both checks pass, which is exactly the pattern for enforcing multiple independent requirements at once.
Example: Logical AND (&&) in Conditions
<?php
// Declare `$age`, set to `20`
$age = 20;
// Declare `$hasConsent`, set to `true`
$hasConsent = true;
// Check whether `$age >= 18 && $hasConsent`
if ($age >= 18 && $hasConsent) {
// Print "Access granted." to the output
echo "Access granted.";
}
?>
Login to try C/C++/Java/PHP code in the editor
Logical OR (||) in Conditions
|| inside a condition lets the block run if *any* one of the joined expressions is true, which suits situations with several acceptable paths to the same outcome — like accepting a login from either a username or an email address.
Example: Logical OR (||) in Conditions
<?php
// Declare `$username`, set to ""
$username = "";
// Declare `$email`, set to "[email protected]"
$email = "[email protected]";
// Check whether `$username || $email`
if ($username || $email) {
// Print "Login accepted." to the output
echo "Login accepted.";
}
?>
Login to try C/C++/Java/PHP code in the editor
Nested if Statements
Placing one if inside another lets you check a second condition only once the first has already passed — useful when the second check genuinely only makes sense in that context (checking a file's contents, say, only after confirming the file exists at all).
Example: Nested if Statements
<?php
// Declare `$fileExists`, set to `true`
$fileExists = true;
// Check whether `$fileExists`
if ($fileExists) {
// Declare `$content`, set to "sample data"
$content = "sample data";
// Check whether `!empty($content)`
if (!empty($content)) {
// Print "File exists and has content." to the output
echo "File exists and has content.";
}
}
?>
Login to try C/C++/Java/PHP code in the editor
- Writing
if ($x = 5)(assignment) instead ofif ($x == 5), which always runs the block. - Putting a semicolon after the condition,
if ($x > 5);, so the following block always executes. - Expecting
if ($str)to be true for"0", when"0"is falsy in PHP.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: