Field Manual PHP / Laravel reference
← PHP

Functions

Functions give a named operation explicit inputs and a return value.

What it is

Functions give a named operation explicit inputs and a return value.

When to use it

Extract repeated logic or business rules that can be tested independently.

Syntax

function add(int $a, int $b): int { return $a + $b; }

Example

function feeCents(int $amountCents): int
{
    if ($amountCents < 0) {
        throw new InvalidArgumentException("Negative amount");
    }
    return intdiv($amountCents, 100);
}

Common mistakes

Avoid hidden global state; a return type is not business validation.

Related topics

Browse PHP