PHP if Statements

In PHP, if statements are used to execute code based on conditions. Here's the basic syntax:

if (condition) { // code to execute if condition is true }

Examples

1. Simple if statement:

$age = 20; if ($age >= 18) { echo "You are an adult."; }

2. if-else statement:

$age = 16; if ($age >= 18) { echo "You are an adult."; } else { echo "You are a minor."; }

3. if-elseif-else statement:

$score = 85; if ($score >= 90) { echo "Grade: A"; } elseif ($score >= 80) { echo "Grade: B"; } elseif ($score >= 70) { echo "Grade: C"; } else { echo "Grade: F"; }

4. Nested if statements:

$age = 25; $hasID = true; if ($age >= 18) { if ($hasID) { echo "You may enter."; } else { echo "You need an ID."; } }