This code defines a function that adds two numbers and returns the sum, then prints the result.
Execution Table
Step
Action
Evaluation
Result
1
Call add(2, 3)
Parameters: a=2, b=3
Function starts
2
Calculate a + b
2 + 3
5
3
Return 5
Return statement reached
5 sent back to caller
4
Assign return to $result
$result = 5
$result holds 5
5
echo $result
Print $result
Output: 5
💡 Function ends after return; value 5 sent back and printed
Variable Tracker
Variable
Start
After Step 1
After Step 4
Final
$a
undefined
2
undefined
undefined
$b
undefined
3
undefined
undefined
$result
undefined
undefined
5
5
Key Moments - 3 Insights
Why does the function stop running after the return statement?
Because return sends a value back and immediately ends the function execution, as shown in step 3 of the execution_table.
What happens to the return value after the function finishes?
The return value replaces the function call in the code, so $result gets assigned the returned value, as seen in step 4.
Can code after return inside the function run?
No, code after return is ignored because return ends the function immediately, shown by no steps after step 3 inside the function.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value does the function add return at step 3?
A2
B5
C3
DUndefined
💡 Hint
Check the 'Result' column in step 3 of the execution_table.
At which step does the variable $result get its value?
AStep 2
BStep 1
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column where $result is assigned in the execution_table.
If the return statement was removed, what would happen to the output?
AOutput would be empty or nothing
BOutput would be 0
COutput would be 5
DNo output or error
💡 Hint
Without return, the function returns nothing, so $result is empty; see variable_tracker for $result values.
Concept Snapshot
Return values in PHP:
- Use 'return' to send a value back from a function.
- Function stops running after return.
- The returned value replaces the function call.
- Assign return to variables to use the result.
- Code after return inside function does not run.
Full Transcript
This example shows how a PHP function returns a value. When add(2, 3) is called, the function runs with parameters a=2 and b=3. It calculates 2 + 3 and returns 5. The return statement ends the function immediately. The returned value 5 is assigned to $result. Then echo prints 5. Variables $a and $b hold 2 and 3 during the function. $result gets 5 after the function returns. Code after return is not executed. This is how return values work in PHP functions.
Practice
(1/5)
1. What does the return statement do inside a PHP function?
easy
A. It declares a new variable inside the function.
B. It prints a value on the screen.
C. It stops the entire script immediately.
D. It sends a value back to where the function was called.
Solution
Step 1: Understand the purpose of return
The return statement 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 D
Quick Check:
Return sends value back [OK]
Hint: Return sends value back to caller, not print or stop script [OK]
Common Mistakes:
Thinking return prints output
Confusing return with exit or die
Assuming return declares variables
2. Which of the following is the correct syntax to return the value 10 from a PHP function?
easy
A. return 10;
B. return(10)
C. return = 10;
D. return: 10;
Solution
Step 1: Recall PHP return syntax
The correct syntax uses the keyword return followed 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 A
Quick Check:
Return value ends with semicolon [OK]
Hint: Return value with semicolon, no assignment or colon [OK]
B. Return statement split incorrectly, returns nothing
C. Variable $product is undefined
D. Function missing closing brace
Solution
Step 1: Analyze the return statement split
The return is 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 B
Quick Check:
Return must be on same line as value [OK]
Hint: Keep return and value on same line to avoid empty return [OK]
Common Mistakes:
Splitting return and value on separate lines
Assuming return reads next line automatically
Ignoring PHP's automatic semicolon insertion
5. You want a PHP function that returns the first non-empty string from an array. Which function correctly returns the first non-empty string or null if none found?
hard
A. function firstNonEmpty(array $arr) {
foreach ($arr as $str) {
if ($str) {
return $str;
}
}
return '';
}
B. function firstNonEmpty(array $arr) {
foreach ($arr as $str) {
if (isset($str)) {
return $str;
}
}
return null;
}
C. function firstNonEmpty(array $arr) {
foreach ($arr as $str) {
if ($str !== '') {
return $str;
}
}
return null;
}
D. function firstNonEmpty(array $arr) {
foreach ($arr as $str) {
if ($str == '') {
return $str;
}
}
return null;
}
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;
} returns null if 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 C
Quick Check:
Return first non-empty string or null [OK]
Hint: Use strict check !== '' to find non-empty strings [OK]
Common Mistakes:
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
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.