So far you have written code that runs once, from top to bottom. But what happens when you need to reuse the same block of logic in multiple places throughout your script? Copying and pasting the same code repeatedly is messy and hard to maintain. This is exactly why PHP Functions exist.
This is Tutorial 6 of 10 in our PHP Basics for Beginners series. In this tutorial, you will learn how to define and call PHP Functions, work with parameters, set default values, and return results from them.
What are PHP Functions?
PHP Functions are reusable blocks of code designed to perform a specific task. Instead of writing the same logic over and over, you define it once inside a function and simply call that function whenever you need it. This makes your code shorter, more organized, and much easier to update later.
PHP already includes many built-in functions like echo, gettype, and count, which you used in earlier tutorials. Now you will learn how to create your own custom PHP Functions from scratch.
Defining a Function
To create one of your own PHP Functions, you use the function keyword, followed by a name you choose, parentheses, and a block of code wrapped in curly braces.
<?php
function greetUser() {
echo "Hello! Welcome to PHP.";
}
?>
At this point, the function only exists as a definition — it will not run automatically. You must call it separately to execute the code inside.
Calling a Function
Calling one of your PHP Functions is simple — just write the function name followed by parentheses wherever you want that code to run.
<?php
function greetUser() {
echo "Hello! Welcome to PHP.";
}
greetUser();
?>
You can call the same function as many times as you want throughout your script, and each time it will run the exact same block of code without forcing you to rewrite it.
Functions with Parameters
Most useful PHP Functions need to work with different values each time you call them. Parameters let you pass data into a function so it can behave differently based on the input it receives.
<?php
function greetUser($name) {
echo "Hello, " . $name . "!";
}
greetUser("Ali");
greetUser("Sara");
?>
Here, the parameter $name accepts a different value each time you call the function, letting the same PHP Functions produce personalized output without duplicating any code.
Multiple Parameters
PHP Functions can accept more than one parameter, separated by commas. This lets you pass several pieces of information into a single function at once.
<?php
function showProfile($name, $age) {
echo $name . " is " . $age . " years old.";
}
showProfile("Ahmed", 28);
?>
Default Parameter Values
Sometimes you want a parameter to fall back to a preset value when nobody provides an argument during the function call. PHP Functions support default values, which you assign directly inside the parentheses during definition.
<?php
function greetUser($name = "Guest") {
echo "Hello, " . $name . "!";
}
greetUser();
greetUser("Bilal");
?>
In this example, calling the function without any argument still works properly because a default value already exists, making your PHP Functions more flexible and error-resistant.
Return Values
Until now, our functions have only echoed output directly. However, many PHP Functions calculate a value and send it back to wherever you called the function, using the return keyword.
<?php
function addNumbers($a, $b) {
return $a + $b;
}
$result = addNumbers(5, 10);
echo $result;
?>
Using return instead of echo inside PHP Functions matters because it lets you store the result in a variable, reuse it in calculations, or pass it into another function entirely.
Variable Scope in Functions
Variables you create inside PHP Functions only exist within that function — this is called local scope. A function cannot directly access a variable you defined outside it, unless you pass that variable in as a parameter.
<?php
$message = "Hello from outside";
function showMessage() {
echo $message;
}
showMessage();
?>
Running the code above will actually trigger an error, because the function cannot see $message from outside its scope. Understanding scope early will help you avoid confusing bugs as your PHP Functions grow more complex.
Why Use Functions?
Using PHP Functions properly keeps your code organized, reduces duplication, and makes debugging significantly easier. When a task repeats across your project, wrapping that logic inside a function always beats copying the same code multiple times. For full technical details, refer to the official PHP functions manual.
What’s Next?
You now understand how PHP Functions work — from definition and calling, to parameters, default values, return statements, and scope. In the next tutorial, we will explore PHP Arrays, where you’ll learn how to store and manage multiple values inside a single variable.
If you missed the previous lesson, check out Tutorial 5: PHP Loops Guide, or explore all lessons in our PHP category.
Practice Exercise
Complete the following tasks to reinforce what you learned in this tutorial:
- Create a PHP file named
functions-practice.php - Write a function called
sayHello()that echoes a welcome message with no parameters - Write a function that accepts a name and age as parameters and echoes a formatted sentence using both
- Write a function with a default parameter value for a country name, then call it once without an argument and once with a custom country
- Write a function called
multiplyNumbers()that returns the product of two numbers instead of echoing it - Store the returned value from
multiplyNumbers()in a variable and echo that variable - Write a function that calculates and returns the area of a rectangle given width and height parameters
Bonus Challenge: Write a function called checkEvenOdd() that accepts a number as a parameter and returns the string “Even” or “Odd” depending on the value, then call it with three different numbers and echo each result.

