0
0
PHPprogramming~10 mins

Return values in PHP - Interactive Code Practice

Choose your learning style9 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.