Part 5 : PHP tutorial for kids and beginners


Part 5: Conditional Statements in PHP

Welcome back to our PHP programming tutorial series! 🎉 In Part 4, we explored arrays in PHP and learned how to create and manipulate different types of arrays. Today, in Part 5, we’ll dive into Conditional Statements. We’ll learn how to use these statements to make decisions in our PHP code. Let’s get started!

What Are Conditional Statements?

Conditional statements allow you to execute different blocks of code based on certain conditions. They are essential for making decisions in your programs and controlling the flow of execution.

Basic Conditional Statements in PHP

PHP offers several types of conditional statements:

  1. if Statement
  2. if-else Statement
  3. if-elseif-else Statement
  4. switch Statement
  5. Ternary Operator

Let’s explore each one in detail.

1. if Statement

The if statement evaluates a condition and executes a block of code if the condition is true.

Syntax:

<?php
    if (condition) {
        // Code to execute if the condition is true
    }
?>

Example:

<?php
    $age = 15;

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

In this example, the message will not display because $age is not greater than or equal to 18.

2. if-else Statement

The if-else statement executes one block of code if the condition is true and another block if the condition is false.

See also  Advanced Form Handling with Alpine.js and Laravel

Syntax:

<?php
    if (condition) {
        // Code to execute if the condition is true
    } else {
        // Code to execute if the condition is false
    }
?>

Example:

<?php
    $age = 15;

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

In this example, the message “You are a minor.” will be displayed because $age is less than 18.

3. if-elseif-else Statement

The if-elseif-else statement checks multiple conditions and executes code based on which condition is true.

Syntax:

<?php
    if (condition1) {
        // Code to execute if condition1 is true
    } elseif (condition2) {
        // Code to execute if condition1 is false and condition2 is true
    } else {
        // Code to execute if none of the above conditions are true
    }
?>

Example:

<?php
    $grade = 85;

    if ($grade >= 90) {
        echo "You got an A!";
    } elseif ($grade >= 80) {
        echo "You got a B!";
    } else {
        echo "You need to work harder.";
    }
?>

In this example, the message “You got a B!” will be displayed because $grade is between 80 and 89.

4. switch Statement

The switch statement is used to perform different actions based on different conditions. It is often used as a more readable alternative to multiple if-else statements.

Syntax:

<?php
    switch (variable) {
        case value1:
            // Code to execute if variable equals value1
            break;
        case value2:
            // Code to execute if variable equals value2
            break;
        default:
            // Code to execute if none of the cases match
    }
?>

Example:

<?php
    $day = "Monday";

    switch ($day) {
        case "Monday":
            echo "Start of the work week!";
            break;
        case "Friday":
            echo "Almost the weekend!";
            break;
        default:
            echo "It's a regular day.";
    }
?>

In this example, the message “Start of the work week!” will be displayed because $day is “Monday”.

See also  Deep Dive into PHP Laravel Routing

5. Ternary Operator

The ternary operator is a shorthand for if-else statements. It evaluates a condition and returns one of two values.

Syntax:

<?php
    $result = (condition) ? value_if_true : value_if_false;
?>

Example:

<?php
    $age = 20;
    $status = ($age >= 18) ? "Adult" : "Minor";
    echo $status; // Outputs: Adult
?>

In this example, the ternary operator checks if $age is 18 or older and sets $status to “Adult” or “Minor”.

Combining Conditional Statements

You can also combine conditions using logical operators:

  • && (AND): Both conditions must be true.
  • || (OR): At least one condition must be true.
  • ! (NOT): Reverses the boolean value of the condition.

Example:

<?php
    $age = 20;
    $hasID = true;

    if ($age >= 18 && $hasID) {
        echo "You can enter.";
    } else {
        echo "You cannot enter.";
    }
?>

In this example, the message “You can enter.” will be displayed if both $age is 18 or older and $hasID is true.

Basic Example

Here’s a PHP script that uses different conditional statements:

<!DOCTYPE html>
<html>
<head>
    <title>Conditional Statements in PHP</title>
</head>
<body>
    <?php
        $age = 15;
        $day = "Friday";

        // if-else statement
        if ($age >= 18) {
            echo "You are an adult.<br>";
        } else {
            echo "You are a minor.<br>";
        }

        // switch statement
        switch ($day) {
            case "Monday":
                echo "Start of the work week!<br>";
                break;
            case "Friday":
                echo "Almost the weekend!<br>";
                break;
            default:
                echo "It's a regular day.<br>";
        }

        // Ternary operator
        $status = ($age >= 18) ? "Adult" : "Minor";
        echo "Status: " . $status . "<br>";
    ?>
</body>
</html>

Summary

In Part 5, we learned about conditional statements in PHP. We explored the if, if-else, if-elseif-else, and switch statements, as well as the ternary operator. We also looked at logical operators to combine conditions. These concepts are crucial for making decisions and controlling the flow of your PHP programs.

See also  Comprehensive guide on logging in Laravel PHP, incorporating example codes, outputs, and explanations:

What’s Next?

In Part 6, we’ll explore Loops in PHP. We’ll learn how to use for, while, and do-while loops to repeat code and perform repetitive tasks.

Homework

  1. Experiment: Create a PHP file and practice using different conditional statements. Try creating complex conditions and using the ternary operator.
  2. Practice: Write a PHP script that checks the current day of the week and displays a message based on whether it’s a weekday or weekend.

Feel free to leave comments if you have any questions or run into any issues. Happy coding! 🚀


Next Part Teaser

Stay tuned for Part 6: Loops in PHP, where we will explore for, while, and do-while loops to repeat tasks and manage repetitive operations in PHP!

Additional Resources

If you want to explore more about conditional statements, check out these resources:


Part 6 Teaser

Coming up next in Part 6: Loops in PHP, where we will explore how to use loops to repeat tasks and manage repetitive operations in PHP!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.