Objective:

The primary objective of this lesson is to introduce learners to the concept of functions in PHP. By the end of this lesson, participants should be able to:

Description:

1.1 Understanding Functions

1.1.1 Introduction to Functions

Understand the role of functions in organizing code, improving reusability, and enhancing maintainability.

1.1.2 Why Use Functions?

Explore the advantages of using functions, such as code modularity, abstraction, and ease of debugging.

1.2 Declaring and Calling Functions

1.2.1 Declaring Functions

Learn how to declare a function in PHP using the function keyword, a function name, and a code block.

<?php
    function greet() {
        echo "Hello, PHP!";
    }
?>

1.2.2 Calling Functions

Explore how to call a function to execute the code within its code block.

<?php
    greet(); // Outputs: Hello, PHP!
?>

1.3 Parameters and Return Values

1.3.1 Function Parameters

Understand the concept of function parameters and how they allow passing values into functions.

<?php
    function greetPerson($name) {
        echo "Hello, $name!";
    }

    greetPerson("John"); // Outputs: Hello, John!
?>

1.3.2 Return Values

Learn how to use the return statement to make functions return values.

<?php
    function add($num1, $num2) {
        return $num1 + $num2;
    }

    $result = add(5, 7);
    echo "Sum: $result"; // Outputs: Sum: 12
?>

1.4 Practice Exercise

Let’s create a simple practice exercise where we’ll define a function to calculate the area of a rectangle. Copy and paste the following code into a PHP file:

<!DOCTYPE html>
<html>
<head>
    <title>Rectangle Area Calculator</title>
</head>
<body>

    <!-- Create a simple form for user input -->
    <form method="POST" action="">
        <label for="length">Enter the length of the rectangle:</label>
        <input type="number" name="length" required>

        <label for="width">Enter the width of the rectangle:</label>
        <input type="number" name="width" required>

        <input type="submit" value="Calculate Area">
    </form>

    <?php
    // PHP code for handling form submission and calculating rectangle area
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // Get user input
        $length = $_POST["length"];
        $width = $_POST["width"];

        // Validate input
        if (!is_numeric($length) || !is_numeric($width) || $length <= 0 || $width <= 0) {
            echo "Please enter valid positive numbers for length and width.";
        } else {
            // Calculate and display the area using a function
            $area = calculateRectangleArea($length, $width);
            echo "<h2>Rectangle Area:</h2>";
            echo "Length: $length<br>";
            echo "Width: $width<br>";
            echo "Area: $area";
        }
    }

    // Function to calculate rectangle area
    function calculateRectangleArea($length, $width) {
        return $length * $width;
    }
    ?>

</body>
</html>

In this exercise:

Copy this code into a PHP file, run it, and test the rectangle area calculator by entering different length and width values.