What if your code could tell you exactly what it just did, every time?
Why Return values in PHP? - Purpose & Use Cases
Imagine you are baking cookies and want to share the recipe results with your friend. You write down each step but never tell your friend what the final cookie tastes like or how many cookies you made. They have to guess or bake themselves to find out.
Without return values, your program can only do tasks but can't tell you what happened or give you results. This means you must repeat work or guess outcomes, which is slow and causes mistakes.
Return values let functions send back results after doing their job. It's like your recipe telling your friend exactly how many cookies were baked and how they taste, so they don't have to guess or redo the work.
$result = add(2, 3); echo "Sum is unknown";
$result = add(2, 3); echo "Sum is $result";
Return values let your code share results, making programs smarter and easier to build step-by-step.
When you order food online, the system returns your order number and estimated delivery time. Without this return info, you'd never know if your order went through or when to expect it.
Return values let functions send back results.
This avoids repeating work and guessing outcomes.
They help build clear, step-by-step programs.
Practice
return statement do inside a PHP function?Solution
Step 1: Understand the purpose of
Thereturnreturnstatement sends a value back to the place where the function was called.Step 2: Differentiate from other actions
It does not print or stop the whole script; it only ends the function and sends back a value.Final Answer:
It sends a value back to where the function was called. -> Option DQuick Check:
Return sends value back [OK]
- Thinking return prints output
- Confusing return with exit or die
- Assuming return declares variables
Solution
Step 1: Recall PHP return syntax
The correct syntax uses the keywordreturnfollowed by the value and a semicolon:return 10;Step 2: Identify syntax errors in other options
A uses invalid assignment; return(10) misses semicolon; D uses colon incorrectly.Final Answer:
return 10; -> Option AQuick Check:
Return value ends with semicolon [OK]
- Omitting semicolon after return
- Using assignment operator with return
- Using colon instead of semicolon
function add($a, $b) {
return $a + $b;
echo "Done";
}
$result = add(3, 4);
echo $result;Solution
Step 1: Understand return stops function execution
Thereturn $a + $b;sends back 7 and stops the function, soecho "Done";is never run.Step 2: Output the returned value
The variable$resultgets 7, andecho $result;prints 7 only.Final Answer:
7 -> Option AQuick Check:
Return stops function, only 7 printed [OK]
- Expecting code after return to execute
- Confusing echo inside function with return
- Thinking return prints output
function multiply($x, $y) {
$product = $x * $y;
return
$product;
}Solution
Step 1: Analyze the return statement split
Thereturnis on one line alone, so PHP treats it as returning nothing immediately.Step 2: Understand effect of line break after return
The next line$product;is unreachable and not part of return, causing function to return null.Final Answer:
Return statement split incorrectly, returns nothing -> Option BQuick Check:
Return must be on same line as value [OK]
- Splitting return and value on separate lines
- Assuming return reads next line automatically
- Ignoring PHP's automatic semicolon insertion
null if none found?Solution
Step 1: Check condition for non-empty string
function firstNonEmpty(array $arr) { foreach ($arr as $str) { if ($str !== '') { return $str; } } return null; } uses$str !== ''to find strings that are not empty exactly.Step 2: Confirm return value if none found
function firstNonEmpty(array $arr) { foreach ($arr as $str) { if ($str !== '') { return $str; } } return null; } returnsnullif no non-empty string is found, which is clear and intentional.Final Answer:
function firstNonEmpty(array $arr) { foreach ($arr as $str) { if ($str !== '') { return $str; } } return null; } -> Option CQuick Check:
Return first non-empty string or null [OK]
- Using loose truthy check that skips '0' string
- Returning empty string instead of null when none found
- Returning on empty string instead of non-empty
Ready to prove it?
Pass the test and this pattern moves unknown
