Bird
Raised Fist0
PHPprogramming~10 mins

Return values in PHP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to return the sum of two numbers.

PHP
<?php
function add($a, $b) {
    return [1];
}

echo add(3, 4);
Drag options to blanks, or click blank then click option'
A$a * $b
B$a - $b
C$a + $b
D$a / $b
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction (-) instead of addition (+).
Forgetting to use the return keyword.
2fill in blank
medium

Complete the code to return the length of a string.

PHP
<?php
function getLength($str) {
    return [1];
}

echo getLength("hello");
Drag options to blanks, or click blank then click option'
Asize($str)
Bstrlen($str)
Clength($str)
Dcount($str)
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() which is for arrays.
Using non-existent functions like size() or length().
3fill in blank
hard

Fix the error in the function to return the first element of an array.

PHP
<?php
function firstElement($arr) {
    return [1];
}

echo firstElement([10, 20, 30]);
Drag options to blanks, or click blank then click option'
A$arr[0]
B$arr(0)
C$arr->0
Darr[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets.
Trying to access array elements as object properties.
4fill in blank
hard

Fill both blanks to return a new array with squares of numbers greater than 3.

PHP
<?php
function squaresAboveThree($numbers) {
    return array_combine(
        array_filter($numbers, fn($n) => $n [1] 3),
        array_map(fn($n) => $n[2]2, array_filter($numbers, fn($n) => $n > 3))
    );
}

print_r(squaresAboveThree([1, 2, 3, 4, 5]));
Drag options to blanks, or click blank then click option'
A>
B*
C**
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication (*) instead of exponentiation for squaring.
Using less than (<) instead of greater than (>) for filtering.
5fill in blank
hard

Fill all three blanks to return an associative array with uppercase keys and values greater than 10.

PHP
<?php
function filterAndUppercase($data) {
    $result = [];
    foreach($data as [3] => [2]) {
        if([2] > 10) {
            $result[[1]] = [2];
        }
    }
    return $result;
}

print_r(filterAndUppercase(['a' => 5, 'b' => 15, 'c' => 20]));
Drag options to blanks, or click blank then click option'
Astrtoupper($key)
B$value
C$key
D$item
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names for keys or values.
Not converting keys to uppercase.

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

  1. Step 1: Understand the purpose of return

    The return statement sends a value back to the place where the function was called.
  2. 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.
  3. Final Answer:

    It sends a value back to where the function was called. -> Option D
  4. 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

  1. Step 1: Recall PHP return syntax

    The correct syntax uses the keyword return followed by the value and a semicolon: return 10;
  2. Step 2: Identify syntax errors in other options

    A uses invalid assignment; return(10) misses semicolon; D uses colon incorrectly.
  3. Final Answer:

    return 10; -> Option A
  4. Quick Check:

    Return value ends with semicolon [OK]
Hint: Return value with semicolon, no assignment or colon [OK]
Common Mistakes:
  • Omitting semicolon after return
  • Using assignment operator with return
  • Using colon instead of semicolon
3. What will be the output of this PHP code?
function add($a, $b) {
  return $a + $b;
  echo "Done";
}
$result = add(3, 4);
echo $result;
medium
A. 7
B. Done7
C. 7Done
D. Error

Solution

  1. Step 1: Understand return stops function execution

    The return $a + $b; sends back 7 and stops the function, so echo "Done"; is never run.
  2. Step 2: Output the returned value

    The variable $result gets 7, and echo $result; prints 7 only.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    Return stops function, only 7 printed [OK]
Hint: Code after return inside function does not run [OK]
Common Mistakes:
  • Expecting code after return to execute
  • Confusing echo inside function with return
  • Thinking return prints output
4. Identify the error in this PHP function:
function multiply($x, $y) {
  $product = $x * $y;
  return
  $product;
}
medium
A. Missing semicolon after return
B. Return statement split incorrectly, returns nothing
C. Variable $product is undefined
D. Function missing closing brace

Solution

  1. Step 1: Analyze the return statement split

    The return is on one line alone, so PHP treats it as returning nothing immediately.
  2. 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.
  3. Final Answer:

    Return statement split incorrectly, returns nothing -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    function firstNonEmpty(array $arr) { foreach ($arr as $str) { if ($str !== '') { return $str; } } return null; } -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard