Limited Offer
60% OFF on all plans!

Functions in PHP

Learn to define and call functions, use default parameters, type declarations, return types, variadic arguments, and anonymous functions in PHP.

Function Basics & Execution Flow

A function is a named, reusable block of code. You define it once and call it as many times as you need. When a function is called, PHP jumps into the function body, executes it, then returns to where it was called from.

1. Creating and Calling a Function

Define once, call many times.

2. Function Execution Flow

PHP jumps into the function and returns back after completion.

Parameters & Arguments

Parameters are the variables listed in the function definition. Arguments are the actual values passed when calling the function. PHP supports default values, type declarations, and named arguments (PHP 8.0+).

1. Default Parameters

Provide a fallback value used when no argument is passed.

2. Named Arguments — PHP 8.0+

Pass arguments by name, allowing any order and skipping defaults.

3. Type Declarations & Return Types

Declare parameter types and return type for safer, self-documenting functions.

4. Nullable Types — PHP 7.1+

Prefix a type with `?` to allow null as a valid value.

Variadic Functions

The `...` (spread/splat) operator collects all remaining arguments into an array. Use it when the number of inputs is not known in advance.

1. Variadic Parameters (...$args)

Collect unlimited arguments into an array.

2. Spreading an Array into a Function

Use `...` at the call site to unpack an array as arguments.

Function Forms

PHP supports named functions, anonymous functions (closures), and arrow functions (PHP 7.4+). Each has different use cases and behavior around variable scope.

1. Anonymous Functions

Functions without names, assignable to variables.

2. Closures with use()

Import outer variables into an anonymous function with `use`.

3. Arrow Functions — PHP 7.4+

Concise single-expression closures that auto-capture outer scope.

4. First-Class Callable Syntax — PHP 8.1+

Get a Closure from any callable using `...`.

Closures & State

A closure can capture a variable by reference, letting it preserve state between calls. This is useful for counters, accumulators, and factory functions.

1. Stateful Closure

Maintain private state by capturing a variable by reference.

2. Closure Factory

Generate customized functions at runtime.

Higher-Order Functions

A higher-order function either accepts a function as an argument, returns a function, or both. To truly understand them, we build common array HOFs from scratch using basic loops, then show PHP's built-in equivalents.

1. What is a Higher-Order Function?

A function that takes a function as argument or returns one.

2. map() from Scratch → array_map()

Transform every element in an array.

3. filter() from Scratch → array_filter()

Keep only elements that pass a test.

4. reduce() from Scratch → array_reduce()

Accumulate an array into a single value.

5. forEach from Scratch → array_walk()

Execute a callback for each element — used for side effects, not transformations.

6. find() from Scratch → array_find()

Return the first element that satisfies a condition.

7. findIndex() from Scratch — array_find_key()

Return the key of the first matching element.

8. some() from Scratch → array_any()

Check if at least one element satisfies a condition.

9. every() from Scratch → array_all()

Check if all elements satisfy a condition.

10. group() from Scratch — Grouping Elements

Group array elements into buckets based on a callback.

11. Sorting with usort()

Sort an array using a custom comparison function.