User-Defined Functions
Create your own reusable functions in PHP.
User-Defined Functions is a free PHP Academy lesson on CoddyKit — lesson 3 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
User-Defined Functions
Welcome to the next lesson! In this lesson, you’ll learn how to create your own functions in PHP to perform specific tasks. User-defined functions allow you to organize and reuse code effectively. Let’s get started!

2
What Are User-Defined Functions?
User-defined functions are functions that you create yourself to perform tasks. They are defined using the function keyword and can include parameters and return values.
Key Point: Functions help you write modular, reusable, and readable code.
3
Defining a Function
To define a function, use the function keyword followed by the function name, parentheses, and curly braces. Example:
Note: The function name should describe the task it performs.
<?php
function greet() {
echo "Hello, PHP!";
}
?>
4
Calling a Function
To use a function, call it by its name followed by parentheses. Example:
<?php
function greet() {
echo "Hello, PHP!";
}
// Call the function
greet();
?>
5
Passing Parameters to Functions
Functions can accept parameters to perform tasks dynamically. Example:
<?php
function greet($name) {
echo "Hello, $name!";
}
greet("Alice"); // Outputs: Hello, Alice!
greet("Bob"); // Outputs: Hello, Bob!
?>
6
Returning Values from Functions
Functions can return a value using the return keyword. Example:
<?php
function add($a, $b) {
return $a + $b;
}
$result = add(5, 10);
echo $result; // Outputs: 15
?>
7
Default Parameter Values
You can set default values for parameters. If no value is passed, the default is used. Example:
<?php
function greet($name = "Guest") {
echo "Hello, $name!";
}
greet(); // Outputs: Hello, Guest!
greet("Alice"); // Outputs: Hello, Alice!
?>
8
Best Practices for User-Defined Functions
Follow these tips for writing effective functions:
- Use meaningful names that describe the function’s purpose.
- Keep functions focused on a single task.
- Document the parameters and return values using comments.
Tip: Modularize your code by breaking down complex tasks into smaller functions.
9
<?php
function multiply($a, $b = 2) {
return $a * $b;
}
echo multiply(5);
?>
10
Great Job!
Congratulations! You’ve learned how to create and use user-defined functions in PHP, including passing parameters, returning values, and setting default parameters. In the next lesson, we’ll explore how to handle arguments and return values more dynamically. Let’s keep coding!

Frequently asked questions
Is the “User-Defined Functions” lesson free?
Yes — the full text of “User-Defined Functions” is free to read here on the web, and the PHP Academy course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “User-Defined Functions”?
Create your own reusable functions in PHP. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 5, so you can start here or from the beginning and move at your own pace.
How long does the “User-Defined Functions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What Are Functions?
- Built-in Functions
- User-Defined Functions
- Passing Arguments to Functions
- Return Values