LearnContact
Lesson 2624 min read

Functions

Learn how to define and call functions in PHP, including parameters, default values, return types, variadic functions, and arrow functions.

Introduction

As programs grow, repeating the same block of code over and over becomes messy and error-prone. Functions let you package a piece of logic under a name, so you can reuse it, test it in isolation, and change it in one place instead of everywhere it is used.

In this lesson you will learn how to define and call functions, use parameters with default values, declare return types, accept a variable number of arguments with variadic parameters, and write short arrow functions introduced in PHP 7.4.

What You Will Learn
  • How to define a function with the function keyword and call it.
  • How to use parameters, including default parameter values.
  • How to return values and declare parameter/return types.
  • How variadic functions accept any number of arguments with ...$args.
  • How arrow functions provide a shorthand syntax for simple functions.

Defining and Calling a Function

A function is defined with the function keyword, a name, a parameter list in parentheses, and a body in braces. Once defined, you call it by writing its name followed by parentheses.

define-call.php
<?php
function greet() {
    echo "Hello there!";
}

greet();
Output
Hello there!

Function names are case-insensitive in PHP (though matching the defined case is good practice), and a function must be defined before it is called, unless it is defined at the top level of a file, in which case PHP makes it available throughout that file.

Parameters and Default Values

Parameters let a function accept input. You can give any parameter a default value, which is used automatically when the caller does not supply that argument.

parameters.php
<?php
function greet($name) {
    echo "Hello, $name!\n";
}

greet("Maria");
greet("Sam");
Output
Hello, Maria!
Hello, Sam!
default-values.php
<?php
function greet($name = "Guest") {
    echo "Hello, $name!\n";
}

greet("Maria");
greet();
Output
Hello, Maria!
Hello, Guest!
Default Parameters Must Come Last

Parameters with default values should be placed after parameters without defaults. PHP allows skipping a default only by name (named arguments) — positionally, once you use a default you cannot go back to a required parameter.

Return Values and Type Declarations

A function sends a result back to the caller using return, which also immediately ends the function's execution. You can optionally declare the type of each parameter and the return type, which helps PHP catch mistakes and makes the function's contract clear to anyone reading it.

return-basic.php
<?php
function add($a, $b) {
    return $a + $b;
}

$sum = add(4, 7);
echo "Sum: $sum";
Output
Sum: 11
typed-function.php
<?php
function add(int $a, int $b): int {
    return $a + $b;
}

echo add(4, 7);
echo "\n";
echo add(2.5, 1);
Output
11
3

In the second call, add(2.5, 1), PHP coerces the float 2.5 down to the int type declared for the parameter (becoming 2) unless strict_types is enabled, in which case a type mismatch like this would throw a TypeError instead.

Variadic Functions

Sometimes you do not know in advance how many arguments will be passed. A variadic parameter, written with three dots before the variable name (...$args), collects any number of trailing arguments into a single array inside the function.

variadic.php
<?php
function sumAll(...$numbers) {
    $total = 0;
    foreach ($numbers as $number) {
        $total += $number;
    }
    return $total;
}

echo sumAll(1, 2, 3);
echo "\n";
echo sumAll(10, 20, 30, 40, 50);
Output
6
150

You can also combine regular parameters with a variadic one, as long as the variadic parameter comes last.

variadic-mixed.php
<?php
function describe($label, ...$items) {
    echo "$label: " . implode(", ", $items);
}

describe("Fruits", "Apple", "Banana", "Cherry");
Output
Fruits: Apple, Banana, Cherry

Arrow Functions

PHP 7.4 introduced arrow functions as a compact shorthand for simple, single-expression functions, using the fn keyword. The result of the expression is returned automatically — there is no need to write return explicitly.

arrow-function.php
<?php
$double = fn($x) => $x * 2;

echo $double(5);
Output
10

Arrow functions also automatically capture variables from the surrounding scope by value, without needing an explicit use clause like regular anonymous functions require. They are especially handy for short callbacks passed to array functions.

arrow-function-array.php
<?php
$numbers = [1, 2, 3, 4, 5];

$squared = array_map(fn($n) => $n * $n, $numbers);

echo implode(", ", $squared);
Output
1, 4, 9, 16, 25

Common Mistakes

Avoid These Mistakes
  • Placing a parameter without a default value after one that has a default.
  • Forgetting that return immediately exits the function — any code after it in the same branch never runs.
  • Assuming type declarations always throw errors on mismatch — without strict_types, PHP coerces compatible scalar types instead.
  • Trying to use multiple statements inside an arrow function body — fn only supports a single expression.
  • Forgetting the three dots when defining a variadic parameter.

Best Practices

  • Give functions clear, descriptive, verb-based names like calculateTotal or isValidEmail.
  • Declare parameter and return types whenever practical for clarity and safety.
  • Use default parameter values to make optional arguments obvious at a glance.
  • Reach for variadic parameters instead of requiring callers to build an array manually.
  • Use arrow functions for short, single-expression callbacks; use regular functions for anything longer.

Frequently Asked Questions

What happens if a function has no return statement?

It implicitly returns null. Calling such a function and using its result will simply give you null.

Can a function have more than one variadic parameter?

No, a function can only have one variadic parameter, and it must be the last one in the parameter list.

Do type declarations make PHP a strictly typed language?

Not by default. PHP performs type coercion for scalar types unless you add declare(strict_types=1) at the top of the file, which enforces exact type matching.

What is the difference between an arrow function and a regular anonymous function?

An arrow function (fn() => ...) is limited to a single expression and automatically captures outer variables by value. A regular anonymous function (function() {...}) supports multiple statements but requires an explicit use() clause to capture outer variables.

Key Takeaways

  • Functions are defined with the function keyword and called by name.
  • Parameters can have default values, used when an argument is omitted.
  • return sends a value back to the caller; type declarations document and enforce expected types.
  • Variadic parameters (...$args) let a function accept any number of trailing arguments.
  • Arrow functions (fn($x) => ...) are a concise, PHP 7.4+ shorthand for simple single-expression functions.

Summary

Functions are the primary way to organize and reuse logic in PHP. You now know how to define and call them, work with parameters and default values, declare types, accept a flexible number of arguments with variadic parameters, and write concise arrow functions for simple cases.

Lesson 26 Completed
  • You can define and call functions with parameters and default values.
  • You can declare parameter and return types.
  • You can write variadic functions and arrow functions.
  • You are ready to learn how variable scope works in PHP.
Next Lesson →

Variable Scope