Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 1 of 8

Named Functions - Parameters, Returns & Type Declarations

Functions are reusable blocks of logic. PHP 8.x allows full type declarations on parameters and return types - making functions self-documenting and catching bugs at call time rather than at runtime. Named arguments (PHP 8.0) let you pass arguments by name in any order.

15 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Function Anatomy - PHP 8.x Style

Function Anatomy - PHP 8.x Style
<?php
declare(strict_types=1);

// ── Basic function ─────────────────────────────────────────────
function greet(string $name): string {
    return "Hello, $name!";
}
echo greet("Alice");     // Hello, Alice!
// greet(42);            // TypeError - strict mode

// ── Default parameters ────────────────────────────────────────
function createSlug(string $title, string $separator = "-"): string {
    $slug = strtolower(trim($title));
    $slug = preg_replace("/[^a-z0-9]+/", $separator, $slug);
    return trim($slug, $separator);
}
echo createSlug("Hello World PHP");         // hello-world-php
echo createSlug("Hello World PHP", "_");    // hello_world_php

// ── Multiple return types (PHP 8.0 union types) ──────────────
function findPost(int $id): array|null {
    $posts = [1 => ["id" => 1, "title" => "PHP Basics"]];
    return $posts[$id] ?? null;
}

// ── Named arguments (PHP 8.0) - clarity on complex calls ──────
function createUser(string $name, string $email, bool $isAdmin = false, int $age = 0): array {
    return compact("name", "email", "isAdmin", "age");
}

// Positional - fragile (wrong order = silent bug):
$user = createUser("Alice", "alice@example.com", false, 28);

// Named - self-documenting, order-independent:
$user = createUser(
    name: "Alice",
    email: "alice@example.com",
    age: 28,
    isAdmin: false,
);

// ── Variadic parameters - accept any number of arguments ──────
function sumAll(int ...$numbers): int {
    return array_sum($numbers);
}
echo sumAll(1, 2, 3, 4, 5);    // 15

// With type enforcement and leading required params:
function logMessage(string $level, string ...$messages): void {
    foreach ($messages as $message) {
        error_log("[$level] $message");
    }
}
logMessage("INFO", "User logged in", "Session started", "Redirecting");

// ── Spread operator - unpack array into args ──────────────────
$args = [3, 4, 5];
echo sumAll(...$args);    // 12  (same as sumAll(3, 4, 5))

// ── Return void - function has no meaningful return ──────────
function savePost(array $post): void {
    // save to database...
    // no return value
}

// ── never return type - function always throws or exits ───────
function fail(string $message): never {
    throw new InvalidArgumentException($message);
}

Quick Quiz

Tip

Tip

Practice Named Functions Parameters Returns Type Declarations in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Named Functions - Parameters, Returns & Type Declarations

Practice Task

Note

Practice Task - (1) Write a working example of Named Functions Parameters Returns Type Declarations from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Named Functions Parameters Returns Type Declarations is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.

Finished this lesson?

0 of 8 in Module 2
Sign in to save progress

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/8 done