devnotes.

Defining and Calling Functions

Functions bundle reusable logic under a name:

function greet(string $name): string
{
    return "Hello, $name!";
}

echo greet('Alice');

Default parameter values:

function greet(string $name, string $greeting = 'Hello'): string
{
    return "$greeting, $name!";
}
greet('Bob');              // "Hello, Bob!"
greet('Bob', 'Welcome');   // "Welcome, Bob!"

Named arguments (PHP 8.0+) let you pass arguments by parameter name, in any order, which is especially useful with many optional parameters:

function createUser(string $name, string $role = 'user', bool $active = true) { ... }
createUser(name: 'Carol', active: false); // skip $role, use its default

Variadic functions accept an arbitrary number of arguments using ...:

function sum(int ...$numbers): int
{
    return array_sum($numbers);
}
sum(1, 2, 3, 4); // 10

Return types and parameter types are optional but strongly recommended for clarity and to catch bugs early — combine with declare(strict_types=1); at the top of a file for fully strict enforcement rather than automatic coercion.

PHP Functions: Parameters, Types, Named Arguments | DevNotes