14. PHP Functions

What you will learn

  • How to create and call a function
  • Parameters, return values, and default values
  • Type declarations
  • Variable scope and arrow functions

What is a function?

A function is a named block of code you can run whenever you need it. PHP has thousands of built-in functions, such as strlen(), and you can write your own.

Creating and calling

PHP code

<?php
function say_hello() {
    echo "Hello from a function!";
}

say_hello();

Output

Hello from a function!

Declaring a function does nothing by itself. It runs only when you call it with brackets.

Parameters and return values

Parameters are the inputs listed in the declaration. return sends a value back and ends the function.

PHP code

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

$total = add(5, 7);
echo "5 + 7 = $total";

Output

5 + 7 = 12

Good functions do one job. Let a function calculate and return a value, and let other code decide how to display it.

Default values

PHP code

<?php
function welcome($name = "friend") {
    return "Welcome, $name!";
}

echo welcome("Sam") . "<br>";
echo welcome();

What the browser shows

Welcome, Sam!
Welcome, friend!

Type declarations

You can say what type each parameter and the return value should be. PHP then catches mistakes early.

PHP code

<?php
declare(strict_types=1);

function area(float $width, float $height): float {
    return $width * $height;
}

echo area(6, 4.5);

Output

27

With declare(strict_types=1); as the first statement of the file, PHP refuses wrong types, such as passing text where a number is required, instead of silently converting. Type declarations make code safer and easier to read. A ?string type means “a string or null”, and void means “returns nothing”.

Named arguments

In PHP 8 you can pass arguments by name, in any order, which makes calls easier to read.

PHP code

<?php
function box(int $width, int $height, string $color = "blue"): string {
    return "$width x $height in $color";
}

echo box(height: 20, width: 10);

Output

10 x 20 in blue

Returning several values

A function returns one value, but that value can be an array.

PHP code

<?php
function min_max(array $numbers): array {
    return [min($numbers), max($numbers)];
}

[$low, $high] = min_max([4, 9, 1, 7]);
echo "Low: $low, High: $high";

Output

Low: 1, High: 9

Scope

A variable made inside a function exists only there, and outside variables are not visible inside. Pass what you need in as parameters. Avoid the global keyword, which hides where values come from.

PHP code

<?php
$rate = 0.2;

function with_tax(float $price, float $rate): float {
    return $price * (1 + $rate);
}

echo with_tax(100, $rate);

Output

120

Arrow functions

For short one-line functions, PHP has arrow functions with fn. They can read outside variables automatically.

PHP code

<?php
$factor = 3;
$triple = fn($n) => $n * $factor;

echo $triple(5) . "<br>";

$numbers = [1, 2, 3];
$doubled = array_map(fn($n) => $n * 2, $numbers);
echo implode(", ", $doubled);

What the browser shows

15
2, 4, 6

array_map() applies a function to every item and returns a new array.

Common mistakes

  • Declaring a function and never calling it.
  • Forgetting return and getting null.
  • Expecting outside variables to be visible inside a function.
  • Declaring the same function name twice, which is a fatal error.
  • Making one huge function that does many unrelated jobs.

Exercise

Write a function rectangle_area($width, $height) that returns the area. Print the result for 6 and 4.

Show solution
<?php
function rectangle_area($width, $height) {
    return $width * $height;
}

echo "Area: " . rectangle_area(6, 4);

The output is Area: 24.

Knowledge check

1. What does a function return if it has no return?

null

2. Are variables from outside a function visible inside it?

No. Pass them in as parameters.

3. What does fn($n) => $n * 2 create?

An arrow function.

Summary

  • Functions package reusable code, with parameters in and return out.
  • Use type declarations and defaults, and pass values in rather than using globals.
  • Arrow functions suit short callbacks.
Scroll to Top