17. Defining Custom Functions
Functions are blocks of code designed to perform a particular task. They help break down complex programs into smaller, manageable, and reusable pieces.
Why Use Functions?
- Reusability: Write once, use everywhere.
- Organization: Improved code structure.
- Readability: Easier to understand the purpose of code blocks.
Function Definition Syntax
We define a function using the function keyword, followed by the function name, parentheses (), and the code block {}.
php
"; displayDate(); ?>Naming Conventions
Function names are case-insensitive, but the recommended convention is camelCase (e.g., calculateTotal, getUserId).
Note on Placement: Functions should be defined before they are called, or included using require_once at the top of the file.