Demystifying Functions

Functions are reusable blocks of code that perform specific tasks, promoting code organization and efficiency.

1. Built-in Function Example

Let's use the built-in `strlen` function to find the length of a string:

    $name = "Kristi";
    $nameLength = strlen($name);
    echo "The name '$name' has $nameLength characters.";
    

2. User-defined Function

Here's a function to calculate the area of a rectangle:

    function calculateRectangleArea($width, $height) {
        return $width * $height;
    }

    $area = calculateRectangleArea(5, 3);
    echo "
The area of a rectangle with width 5 and height 3 is $area square units.";

This function takes two arguments (width and height) and returns the calculated area.

Understanding Function Concepts

Functions enable code reusability, modularity, and cleaner organization, making your code more readable and maintainable.