Return Values
Retrieve results from functions in PHP.
Return Values is a free PHP Academy lesson on CoddyKit — lesson 5 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
Return Values
Welcome to the next lesson! In this lesson, you’ll learn about return values in PHP functions and how they allow you to pass the results of a function back to the calling code. Let’s dive in!

2
What Are Return Values?
A return value is the result of a function’s execution. Instead of outputting data directly, a function can return it to be used elsewhere in the program.
Example: A function that calculates the area of a rectangle can return the result to the caller.
3
Using the return Keyword
To return a value from a function, use the return keyword. Example:
Tip: Use return values to pass data from a function back to the calling code.
<?php
function add($a, $b) {
return $a + $b;
}
$result = add(5, 10);
echo $result; // Outputs: 15
?>
4
Returning Strings
Functions can return strings, which can be concatenated or displayed. Example:
Task: Write a function that returns a personalized greeting message.
<?php
function greet($name) {
return "Hello, $name!";
}
echo greet("Alice"); // Outputs: Hello, Alice!
?>
5
Returning Arrays
Functions can return arrays, which are useful for passing multiple values. Example:
Tip: Use return to pass complex data structures like arrays.
<?php
function getColors() {
return ["Red", "Green", "Blue"];
}
$colors = getColors();
echo $colors[0]; // Outputs: Red
?>
6
Returning Multiple Values
To return multiple values, use an associative array or an object. Example:
Key Point: Use associative arrays to return named values.
<?php
function getUserInfo() {
return [
"name" => "Alice",
"age" => 25,
"city" => "New York"
];
}
$user = getUserInfo();
echo $user["name"]; // Outputs: Alice
?>
7
Returning Conditional Values
Functions can return different values based on conditions. Example:
Tip: Use return to handle conditional logic inside functions.
<?php
function checkEligibility($age) {
if ($age >= 18) {
return "Eligible";
} else {
return "Not Eligible";
}
}
echo checkEligibility(20); // Outputs: Eligible
?>
8
Type Declarations for Return Values
PHP allows you to declare the return type of a function. Example:
Key Point: Return type declarations help ensure your functions return the expected data type.
<?php
function multiply(float $a, float $b): float {
return $a * $b;
}
echo multiply(2.5, 4); // Outputs: 10.0
?>
9
<?php
function subtract($a, $b) {
return $a - $b;
}
echo subtract(10, 5);
?>
10
Great Job!
Congratulations! You’ve learned how to use return values in PHP functions, including returning strings, arrays, and handling multiple or conditional values. In the next lesson, we’ll explore lambda functions and anonymous functions. Let’s keep coding!

Frequently asked questions
Is the “Return Values” lesson free?
Yes — the full text of “Return Values” 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 “Return Values”?
Retrieve results from 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 5 of 5, so you can start here or from the beginning and move at your own pace.
How long does the “Return Values” 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.