Example 4: Age Verification

if-else Conditional structures PHP

Download file edad

🎯 Live Demo

Verification result:

21 years old
You are an adult!
✅ Adult

Try different ages!

Age between 0 and 120 years old

Test ages:

📖 Code Explanation

Complete PHP Code

<?php
$edad = 21;

if ($edad >= 18) {
    echo "You are an adult!";
} else {
    echo "You are still a minor";
}
?>

📝 Line 1: Variable declaration

$edad = 21;

What does it do? Creates a numeric variable that stores the age to verify.

Why is it done this way? Ages are numeric values, so they are assigned without quotes to allow math operations.

💡 Concept: Numeric data types

In PHP, numbers can be:
Integer: Whole numbers (21, 18, 0)
Float: Decimal numbers (21.5, 18.0)
For ages we normally use integers.

📝 Lines 3-7: if-else conditional structure

if ($edad >= 18) {
echo "You are an adult!";
} else {
echo "You are still a minor";
}

What does it do? Evaluates whether the age is greater than or equal to 18 and executes different blocks of code depending on the result.

Why is it done this way? if-else structures allow making decisions in the code based on conditions.

💡 Concept: Comparison operators

>= Greater than or equal to
> Greater than
<= Less than or equal to
< Less than
== Equal to
!= Not equal to

📝 Execution flow

1
The condition is evaluated $edad >= 18
2
If it's TRUE → Executes the block if
3
If it's FALSE → Executes the block else

The program only executes one of the two blocks, never both.

🔄 Conditional Structures in PHP

Simple if

if ($condicion) {
    // código si es verdadero
}

Executes code only if the condition is true.

if-else

if ($condicion) {
    // código si es verdadero
} else {
    // código si es falso
}

Executes one block or the other depending on the condition.

if-elseif-else

if ($condicion1) {
    // código 1
} elseif ($condicion2) {
    // código 2
} else {
    // código por defecto
}

Multiple conditions in sequence.

⚖️ Comparison Operators

Operator Name Example Result
== Equal 5 == 5 TRUE
=== Identical 5 === "5" FALSE
!= Different 5 != 3 TRUE
> Greater than 5 > 3 TRUE
< Less than 5 < 3 FALSE
>= Greater or equal 5 >= 5 TRUE

🎯 Key Concepts Learned

Control Structures

  • Allow making decisions
  • Control the program flow
  • Based on boolean conditions
  • Essential for programming logic

Boolean Conditions

  • Can only be TRUE or FALSE
  • Evaluated with comparison operators
  • Can be combined with AND/OR
  • Determine which code runs

Code Blocks

  • Enclosed in braces {}
  • Each block has its own scope
  • Improve readability
  • Allow multiple statements

💪 Practical Exercise

Extend the verification system

Practice these changes to improve your skills:

  1. Add a category for "senior citizen" (65 years or older)
  2. Create age ranges: child (0-12), teenager (13-17), adult (18-64), senior (65+)
  3. Implement validation for negative or very high ages
  4. Add a special message for those who are exactly 18 years old
  5. Create a system that also checks if they can vote (18+) and drive (16+)

Solution 1: senior citizen

<?php
if ($edad >= 65) {
    echo "You are a senior citizen";
} elseif ($edad >= 18) {
    echo "You are an adult!";
} else {
    echo "You are still a minor";
}
?>

Solution 2: Age ranges

<?php
if ($edad >= 65) {
    echo "You are a senior";
} elseif ($edad >= 18) {
    echo "You are an adult";
} elseif ($edad >= 13) {
    echo "You are a teenager";
} else {
    echo "You are a child";
}
?>

Solution 3: Validation

<?php
if ($edad < 0 || $edad > 120) {
    echo "Invalid age";
} elseif ($edad >= 18) {
    echo "You are an adult!";
} else {
    echo "You are still a minor";
}
?>

Solution 4: Exactly 18 years old

<?php
if ($edad == 18) {
    echo "You just became an adult!";
} elseif ($edad > 18) {
    echo "You are an adult!";
} else {
    echo "You are still a minor";
}
?>

Solution 5: Multiple checks

<?php
if ($edad >= 18) {
    echo "You can vote and drive";
} elseif ($edad >= 16) {
    echo "You can drive but not vote";
} else {
    echo "You cannot vote or drive";
}
?>