Functions are reusable blocks of code designed to perform a specific task. They allow you to write a piece of logic once and call it multiple times throughout your application, keeping your codebase clean, organized, and DRY (Don't Repeat Yourself).
Defining and Calling Functions
To create a function, use the function keyword, followed by a unique name, a pair of parentheses (), and a block of code wrapped in curly braces { ... }.
<?php
function sayHello()
{
echo "Hello from the function!";
}
// Calling the function
sayHello(); // Outputs: Hello from the function!
Note: Unlike variable names, function names in PHP are case-insensitive (so sayhello() would still work), but it is a universal best practice to always call them exactly as they are defined.
Functions become significantly more useful when you can pass data into them. You can achieve this by defining parameters inside the parentheses. Think of parameters as local variables that only exist inside the function.
When you actually invoke (call) the function, the values you pass into it are called arguments.
<?php
function greetUser($name)
{
echo "Welcome back, " . $name . "!";
}
greetUser("Alice"); // Outputs: Welcome back, Alice!
greetUser("Bob"); // Outputs: Welcome back, Bob!
While some functions output text directly using echo, you'll often want a function to process data and pass the result back to the code that called it. We use the return keyword to accomplish this.
<?php
function multiply($a, $b)
{
return $a * $b;
}
$total = multiply(5, 4);
echo $total; // Outputs: 20
It's critical to know that the return statement immediately stops the execution of a function. Any code written inside the function block after a return statement will be completely ignored.
You can make parameters optional by giving them a default value directly in the function definition. If the caller doesn't provide an argument for that parameter, PHP will seamlessly fall back to your default.
<?php
function connectToDatabase($host = "localhost")
{
echo "Connecting to database at: " . $host;
}
connectToDatabase("192.168.1.5"); // Outputs: Connecting to database at: 192.168.1.5
connectToDatabase(); // Outputs: Connecting to database at: localhost
Note: Once you use one default argument, every other argument after it in that function must also have a default value.
Tying back to what we learned about Variable Types, modern PHP allows you to enforce strict data types for both your function parameters and your return values. This prevents unexpected type-juggling bugs and makes your code self-documenting.
To enforce types, prefix the parameters with the type name, and add a colon : followed by the return type right before the opening curly brace.
<?php
function calculateTotal(float $price, int $quantity): float
{
return $price * $quantity;
}
echo calculateTotal(10.50, 3); // Outputs: 31.5
If you try to pass an incompatible type—such as an array or a non-numeric string into a typed parameter, PHP will halt execution and throw a TypeError, helping you catch bugs early in development, and protecting you from operating on unexpected data.