Functions in PHP are the fundamental way to organize code. They allow you to group instructions, give them a name, and call them multiple times across your program. With functions, your code becomes cleaner, shorter, easier to test, and reusable. The return statement is used to return a result from a function and stop its execution.
In this lesson (the first about functions in this course), you’ll learn:
- how to define functions in PHP,
- how return works,
- how to use parameters, default values, types, and variable scope,
- what are the best practices and common mistakes.
Basics
What is a function?
A function is a named block of code that can take parameters, perform operations, and (optionally) return a value. You can call a function many times, which helps avoid code duplication.
Function syntax in PHP
General pattern:
<?php
function functionName($optionalParameters) {
// function body
// ...
return $value; // optional
}
- function – keyword for defining a function,
- functionName – the name (use lowerCamelCase or snake_case),
- parameters – variables received by the function,
- return – returns the result and ends execution of the function (can be omitted).
Parameters and arguments
- Parameters – variables defined in the function header (e.g., \$a, \$b).
- Arguments – values passed when calling (e.g., 2, 3).
- You can set default values for parameters.
The return statement
- return ends execution of the function and optionally returns a value.
- Code after return will never be executed (it’s unreachable).
- You can have multiple return paths (e.g., in if/else).
Parameter types and return type
Since PHP 7+ and 8+, you can declare types:
- Parameters:
function add(int $a, int $b) { ... }
- Return type:
function add(int $a, int $b): int { ... }
- Types:
int
,float
,string
,bool
,array
,object
,callable
,mixed
,void
,never
, union typesint|string
, nullable types?string
. - Declaration
declare(strict_types=1);
enforces strict argument typing.
Variable scope
- Variables declared in a function are local to it.
- Variables outside are not visible inside (unless you use
global
or pass them as parameters). - Prefer parameters instead of
global
– it’s a better practice.
PHP Code Examples
1) Simplest function without parameters and return
<?php
function greet() {
echo "Hello, PHP!\n"; // prints text but doesn’t return a value
}
greet(); // call function
2) Function returning a value (return)
<?php
function add($a, $b) {
return $a + $b; // returns sum and ends function
}
$result = add(2, 3);
echo $result; // 5
3) Function with parameter and return types
<?php
declare(strict_types=1);
function multiply(int $a, int $b): int {
return $a * $b;
}
echo multiply(4, 5); // 20
// echo multiply("4", 5); // Fatal error if strict_types=1
4) Default parameter values
<?php
function greeting(string $name = "Guest"): string {
return "Hello, $name!";
}
echo greeting(); // Hello, Guest!
echo "\n";
echo greeting("Alice"); // Hello, Alice!
5) Nullable type and early return (guard clause)
<?php
function formatName(?string $firstName, ?string $lastName): string {
if ($firstName === null || $lastName === null) {
return "No data";
}
return $lastName . ", " . $firstName;
}
echo formatName("John", "Doe"); // Doe, John
echo "\n";
echo formatName(null, "Doe"); // No data
6) Variadic parameter (...\$param) – unlimited arguments
<?php
function sumAll(int ...$numbers): int {
$sum = 0;
foreach ($numbers as $n) {
$sum += $n;
}
return $sum;
}
echo sumAll(1, 2, 3, 4); // 10
7) Multiple return paths (if/else)
<?php
function grade(int $points): string {
if ($points >= 90) return "excellent";
elseif ($points >= 75) return "very good";
elseif ($points >= 60) return "good";
return "pass";
}
echo grade(78); // very good
8) Returning an array from a function
<?php
function splitName(string $fullName): array {
$parts = explode(" ", trim($fullName));
$first = $parts[0] ?? "";
$last = $parts[1] ?? "";
return [$first, $last];
}
[$first, $last] = splitName("John Smith");
echo $first . " | " . $last; // John | Smith
9) Void function and return without value
<?php
function logMessage(string $msg): void {
echo "[LOG] $msg\n";
return; // optional
}
logMessage("Program started");
10) Function with never return type
<?php
function stopWithError(string $msg): never {
throw new RuntimeException($msg);
// or exit($msg);
}
// stopWithError("Something went wrong");
11) Arrow function (PHP 7.4+)
<?php
$square = fn(int $x): int => $x * $x;
echo $square(6); // 36
12) Passing by reference
<?php
function incrementByOne(int &$x): void {
$x = $x + 1;
}
$num = 10;
incrementByOne($num);
echo $num; // 11
13) Recursion
<?php
function factorial(int $n): int {
if ($n < 0) throw new InvalidArgumentException("n must be >= 0");
if ($n === 0) return 1;
return $n * factorial($n - 1);
}
echo factorial(5); // 120
14) Named arguments (PHP 8+)
<?php
function sendMail(string $to, string $subject, string $body): bool {
return true;
}
sendMail(
to: "contact@example.com",
subject: "Question",
body: "Hello!"
);
15) Echo vs return
<?php
function buildGreeting(string $name): string {
return "Hello, $name!";
}
function printGreeting(string $name): void {
echo "Hello, $name!";
}
$text = buildGreeting("Ola");
echo $text;
printGreeting("Ola");
Best Practices and Common Mistakes
Best Practices
- Give clear names to functions and parameters (e.g.,
calculateTax
,fetchUser
). - One function = one responsibility (SRP).
- Prefer returning values over side effects (echo, global).
- Use parameter and return types (standard in PHP 8+).
- Provide default values for optional parameters, placed at the end.
- Use early returns for validation (guard clauses).
- Document functions (DocBlock) and write unit tests for key logic.
- Avoid global — prefer parameters or objects.
- Keep function contracts consistent: predictable inputs/outputs.
- Split long functions into smaller ones.
Common Mistakes
- Missing
return
where a value is expected. - Code after
return
— unreachable. - Type mismatch with declared types.
- Confusing echo with return.
- Missing or misordered arguments.
- Null errors — use nullable types
?
and checks. - Overusing references
&
. - Recursion without base case.
- Relying on globals.
- Missing types and validations.
Summary
- Functions let you write modular, reusable code.
- return ends function and optionally returns a value.
- Use types, defaults, variadics, nullable types.
- Respect variable scope; prefer parameters.
- Use early returns, avoid side effects, test and document.
Mini Quiz
- What does return do in PHP functions?
- Output?
function f() { return 3; echo 5; }
echo f();
➡️ 3
- Correct definition?
function sum(int $a, int $b): int { return $a + $b; }
- Difference echo vs return?
- Which definition of optional parameter is correct?
function x($a, $b = 10) { ... }
- ?string means?
- Output of g(2,3,4)?
- Function with void return type?
- Code after return runs?
- never type means?
You now know the basics of functions and return in PHP. Practice by creating your own functions: simple calculations, validation with early return, functions with types and defaults. This will make your PHP code more professional and efficient.