PHP Reference ยท Functions and Classes
Defining functions in PHP
Reference for declaring PHP functions: the function keyword, typed parameters, return types, void and never, hoisting, conditional functions and naming rules.
A function is a named block of code you can call as often as you like. You declare one with the function keyword, a name, a parameter list in parentheses and a body in braces. A return statement hands a value back to the caller; a function that reaches the end without return gives back null.
Parameters and return values can carry types. Types document the function, let PHP reject wrong arguments with a TypeError, and help editors and static analyzers catch mistakes before the code runs.
function area(float $width, float $height): float { return $width * $height; } echo area(3, 4.5); // 13.5
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
function name(type $param1, type $param2 = default, type ...$rest): returnType { // body return $value; }
| Part | Description |
|---|---|
name |
Letters, digits and underscores, not starting with a digit. Names are case-insensitive for functions, but use one spelling. |
| Parameter type | Optional. A class, int, float, string, bool, array, callable, iterable, object, mixed, nullable (?int), union (int|string) or intersection types. |
| Default value | Makes a parameter optional. See Default parameter values. |
&$param |
Passes by reference. See Passing by reference. |
...$rest |
Collects extra arguments. See Variadic functions. |
| Return type | After a colon. void returns nothing, never never returns (always throws or exits), static and self are allowed in methods. |
Return value: whatever the return statement gives, checked against the return type. A function with a non-nullable return type that returns the wrong type throws TypeError (with a coercion first when strict_types is off).
Errors: a missing required argument throws ArgumentCountError; calling an undefined function throws Error ("Call to undefined function").
Usage notes
- Hoisting. Functions declared at the top level of a file can be called before the line where they appear. Functions declared inside an
ifor inside another function exist only after that code has run. - No overloading. You cannot declare two functions with the same name and different parameters. Use default values, union types or variadics instead.
- Function names are global (or live inside the current namespace). Avoid clashes with built-in names; use
function_exists()for optional polyfills. declare(strict_types=1)applies to calls made from that file. Without it PHP converts compatible scalars ("5"to5); with it, a wrong scalar type throws.- One value out. To return several values, return an array or object and unpack with
[$a, $b] = f();. - Return type
voidforbidsreturn $x;and the call evaluates tonull. Do not use the result. - Scope. A function cannot see variables from the calling code; pass them as arguments (see Variable scope).
- Pure functions that depend only on their arguments are the easiest to test and reuse.
- Arguments are passed by value for scalars and arrays (copy-on-write, so cheap); objects are handles, so changes through the handle are visible to the caller.
- Named arguments (PHP 8.0) let callers write
area(height: 2, width: 3); parameter names are therefore part of your public API.