Back to course

Defining Custom Functions

PHP: The Complete 0 to Hero Bootcamp

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?

  1. Reusability: Write once, use everywhere.
  2. Organization: Improved code structure.
  3. 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.