PHP Functions

A function is a block of code that does one job, and you can use it again and again by calling its name. Functions make your code shorter, cleaner and easy to fix. PHP has more than a thousand built-in functions (like strlen()), and you can also create your own.


Create and call a function

A function starts with the function keyword, then the name, brackets and a body in curly braces. It does not run until you call it.

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

sayHello();
?>

Output:

Tutorials Class - Output Window

Hello from a function!


Parameters and return value

Parameters are values that you give to the function. The return statement sends a value back. A parameter can have a default value, which is used when you do not pass anything.

<?php
function greet($name = "Guest") {
    return "Hello, " . $name;
}

echo greet("Robin") . "<br>";
echo greet();
?>

Output:

Tutorials Class - Output Window

Hello, Robin
Hello, Guest


Type declarations

You can write the type of the parameters and the return value. PHP then checks the values, and it gives an error if the type is wrong. It makes the code safer and easy to understand.

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

echo add(2, 3);
?>

Output:

Tutorials Class - Output Window

5


Named arguments (PHP 8)

From PHP 8 you can pass values using the parameter names, in any order.

<?php
function volume($width, $height, $depth) {
    return $width * $height * $depth;
}

echo volume(height: 2, width: 3, depth: 4);
?>

Output:

Tutorials Class - Output Window

24


More ways to use functions

Variable number of arguments

<?php
function sum(...$numbers) {
    return array_sum($numbers);
}

echo sum(1, 2, 3, 4);
?>

Output:

Tutorials Class - Output Window

10

Pass by reference

Normally PHP gives a copy of the variable to the function. With an ampersand (&) the function changes the original variable.

<?php
function addOne(&$number) {
    $number++;
}

$x = 5;
addOne($x);

echo $x;
?>

Output:

Tutorials Class - Output Window

6

Return many values

<?php
function minMax(array $list) {
    return [min($list), max($list)];
}

[$min, $max] = minMax([4, 9, 2]);

echo $min . " " . $max;
?>

Output:

Tutorials Class - Output Window

2 9

Anonymous and arrow functions

A function without a name can be saved in a variable or passed to another function. An arrow function (fn) is a short form for simple one-line functions.

<?php
$hello = function ($name) {
    return "Hi " . $name;
};
echo $hello("Anil") . "<br>";

$double = fn($n) => $n * 2;
echo $double(4);
?>

Output:

Tutorials Class - Output Window

Hi Anil
8


Summary

  • Create a function with function name() { } and call it by its name.
  • Use parameters to give data and return to get a result.
  • Use type declarations, default values and named arguments to write clear code.
  • A variable inside a function is local. Read about scope in PHP Variables.

Related: the list of PHP built-in functions, and the constant() function.


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
Factorial program in PHP using recursive function
Write a PHP program to calculate area of rectangle
Interview Questions & Answers
What is PHP Function?
What is PHP floor() function?
How to count the number of parameters passed in a PHP function?
How can you pass a variable by reference in PHP?