PHP Tutorial

Functions

Defining functions, parameters, and type hinting in PHP

Defining Functions

In PHP, you define a function using the function keyword.
php
function sum($a, $b) {
    return $a + $b;
}

Type Hinting (Modern PHP)

Wait! In modern PHP (7.0+), you can and should specify the types of your parameters and your return value. This makes your code much more reliable.
php
function sum(int $a, int $b): int {
    return $a + b;
}

Default Parameters

You can provide default values for parameters, making them optional when the function is called.
php
function greet(string $name, string $prefix = "Hello"): string {
    return "$prefix, $name!";
}

Named Arguments (PHP 8+)

Wait! PHP 8 introduced Named Arguments, allowing you to pass values to a function based on the parameter name, not just the position.
php
greet(prefix: "Hi", name: "Alpha");

Anonymous Functions (Closures)

You can create functions that don't have a name. These are often used as callbacks.
php
$greet = function($name) {
    echo "Hello $name";
};

$greet("Alpha");

Summary

TermDescription
functionKeyword to declare a function
returnProviding a value back to the caller
voidReturn type for functions that return nothing
mixedType hint for a value that can be anything
ClosureAn anonymous function that can "use" outer variables
Arrowfn() => ... (Short closures in PHP 7.4+)
Best ForLogic reusability and organization
LogicSmall functions are easier to test and maintain
SafetyUse type hints for everything to avoid bugs
ModernPHP 8 features make functions much more powerful
StandardUse camelCase for function names
IdentityFunctions in PHP are not case-sensitive (but call them correctly!)