Home Quizzes Leaderboard Competitions Learn Hire Us
About Contact
Log In Sign Up
Learn PHP Functions

Functions

⏱ 15 min read read
Defining Functions:

function greet(string $name): void {

echo "Hello, $name!";

}

function add(int $a, int $b): int {

return $a + $b;

}

PHP 7+ supports type hints for parameters and return types.

Type hints make code safer and self-documenting.

Use : void for functions that return nothing.

Use ?string for nullable types (string or null).

Default Arguments:

function createProfile(string $name, string $city = "Unknown",
string $role = "User"): string {

return "$name from $city [$role]";

}

echo createProfile("Alice", "Lagos", "Admin");

echo createProfile("Bob"); // uses defaults

Variadic Functions (...$args):

function addAll(int ...$numbers): int {

return array_sum($numbers);

}

echo addAll(1, 2, 3); // 6

echo addAll(10, 20, 30, 40); // 100

Anonymous Functions & Arrow Functions:

// Anonymous function

$square = function(int $x): int { return $x ** 2; };

echo $square(5); // 25

// Arrow function (PHP 7.4+) --- cleaner syntax

$square = fn(int $x): int => $x ** 2;

echo $square(5); // 25

// Arrow functions capture outer variables automatically

$multiplier = 3;

$triple = fn($x) => $x * $multiplier; // $multiplier captured

echo $triple(5); // 15

Pass by Reference:

function addTen(int &$n): void {

$n += 10;

}

$value = 5;

addTen($value);

echo $value; // 15 --- modified in place
Code Example
<?php

// Basic function with type hints

function greet(string $name): void {

echo "Hello, $name!\n";

}

function add(int $a, int $b): int {

return $a + $b;

}

function isEven(int $n): bool {

return $n % 2 === 0;

}

// Default arguments

function createProfile(string $name, int $age, string $city =
"Unknown", string $role = "User"): string {

return "$name, $age, from $city [$role]";

}

echo createProfile("Alice", 25, "Lagos", "Admin") . "\n";

echo createProfile("Bob", 30) . "\n";

// Variadic --- any number of args

function addAll(int ...$numbers): int {

return array_sum($numbers);

}

echo addAll(1, 2, 3) . "\n"; // 6

echo addAll(10, 20, 30, 40) . "\n"; // 100

// Multiple return values (array)

function analyze(array $numbers): array {

return [min($numbers), max($numbers), array_sum($numbers) /
count($numbers)];

}

[$lo, $hi, $avg] = analyze([3, 7, 1, 9, 4, 6]);

echo "Min: $lo, Max: $hi, Avg: $avg\n";

// Arrow function (PHP 7.4+)

$square = fn(int $x): int => $x ** 2;

echo $square(5) . "\n"; // 25

// Lambda with usort

$students =
[["name"=>"Alice","score"=>92],["name"=
>"Bob","score"=>78],["name"=>"Carol","score"=>85]];

usort($students, fn($a, $b) => $b['score'] <=>
$a['score']);

foreach ($students as $s) {

echo "{$s['name']}: {$s['score']}\n";

}

// Temperature converter

function celsiusToFahrenheit(float $c): float {

return ($c * 9/5) + 32;

}

echo "0C = " . celsiusToFahrenheit(0) . "F\n"; // 32

echo "100C = " . celsiusToFahrenheit(100) . "F\n"; // 212

?>
← Arrays Object-Oriented Programming --- Classes →

Log in to track your progress and earn badges as you complete lessons.

Log In to Track Progress