A PHP function is defined with the function keyword, a name, a parameter list and a body; return sends a value back to the caller. function add(int $a, int $b): int { return $a + $b; } defines it, and add(2, 3) calls it and returns 5.
The string and int before the parameters are type declarations, and : string after the parentheses is the return type. Both are optional, but they catch mistakes early and document what the function expects.
Return values and returning several values
return ends the function immediately. A function without return, or with a bare return;, returns null; declare : void when it is called only for its effect. A function returns one value, but that value can be an array, which you destructure on the calling side:
See array destructuring for the other forms of [...] =.
Default parameters and named arguments
A parameter with = value is optional: callers can leave it out. Optional parameters go after the required ones. Since PHP 8.0, named arguments let the caller pass any parameter by name, so you can skip the optional ones you do not need.
Named arguments make calls to built-in functions readable too: str_pad('7', 3, '0', pad_type: STR_PAD_LEFT). The parameter name becomes part of the function's public interface, so renaming a parameter can break callers that use names.
Pass by reference
Normally PHP passes a copy of each argument, so the function cannot change the caller's variable. An & before a parameter passes the variable itself. Built-in functions like sort() work this way.
Returning a new value is usually clearer than changing an argument. Use references when the function really is meant to modify something in place.
Type declarations and strict_types
By default PHP converts arguments to the declared type when it can: add("5", 3) turns "5" into 5. Putting declare(strict_types=1); at the very top of the file turns that off for calls made in that file, so a wrong type throws a TypeError instead of being converted.
Without the declare line, the same call prints 8. Strings that are not numbers, like "five", throw a TypeError in both modes.
The types you can declare for parameters and return values:
never is a return type for functions that always throw or exit. Class and interface names work as types too.
Variable number of arguments with ...$args
...$numbers collects any number of arguments into an array. The same ... on the calling side spreads an array into separate arguments.
Using an outside variable inside a function
A function has its own scope. A variable defined outside it is not visible inside, which surprises people coming from JavaScript:
<?php
$taxRate = 0.2;
function priceWithTax(float $price): float
{
return $price * (1 + $taxRate); // $taxRate is undefined here
}
Warning: Undefined variable $taxRate in /path/to/index.php on line 6
and the function returns the price without tax. Pass the value in as a parameter instead. That keeps the function honest about what it depends on and easy to test:
The global keyword also works, but hides the dependency; variable scope covers it. For short functions passed to other functions, like callbacks for array_map(), see anonymous functions, which can capture outside variables with use.
Frequently Asked Questions
How do I define a function in PHP?
Use the function keyword, a name, parameters in parentheses and a body: function add(int $a, int $b): int { return $a + $b; }. Call it as add(2, 3). Function names are case-insensitive, and a function defined at the top level of a file can be called before the line that defines it.
How do I return multiple values from a PHP function?
Return an array and destructure it: function minMax(array $a): array { return [min($a), max($a)]; } then [$min, $max] = minMax([3, 1, 4]);. For named results, return an associative array and use ['min' => $min] = ....
What does declare(strict_types=1) do?
It turns off automatic type conversion for calls made from that file, so passing "5" to an int parameter throws a TypeError instead of converting it to 5. It must be the very first statement in the file and only affects that file.
What are named arguments in PHP?
Since PHP 8.0 you can pass arguments by parameter name: htmlspecialchars($s, double_encode: false). They can be given in any order after the positional ones, and let you skip optional parameters you do not want to change.
How do I pass a variable by reference in PHP?
Put & before the parameter: function addOne(int &$n): void { $n++; }. The function then changes the caller's variable. Without &, PHP passes a copy and the caller's variable stays the same.