Bird
Raised Fist0
PHPprogramming~10 mins

Foreach loop for arrays 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 loop through the array and print each fruit.

PHP
<?php
$fruits = ['apple', 'banana', 'cherry'];
foreach ($fruits as [1]) {
    echo $fruit . "\n";
}
?>
Drag options to blanks, or click blank then click option'
A$fruit
B$fruits
C$item
D$value
Attempts:
3 left
💡 Hint
Common Mistakes
Using the array variable itself instead of a single element variable.
Using a variable name that is not defined in the loop.
2fill in blank
medium

Complete the code to loop through the array with keys and values.

PHP
<?php
$ages = ['Alice' => 25, 'Bob' => 30];
foreach ($ages as [1] => $age) {
    echo "$name is $age years old.\n";
}
?>
Drag options to blanks, or click blank then click option'
A$value
B$name
C$item
D$key
Attempts:
3 left
💡 Hint
Common Mistakes
Using the value variable instead of the key variable.
Using a variable name that does not match the echo statement.
3fill in blank
hard

Fix the error in the foreach loop to correctly print keys and values.

PHP
<?php
$colors = ['red' => '#FF0000', 'green' => '#00FF00'];
foreach ($colors as $color [1] $hex) {
    echo "$color: $hex\n";
}
?>
Drag options to blanks, or click blank then click option'
A->
B=
C:
D=>
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment operator = instead of =>.
Using object operator -> or colon : which are incorrect here.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 letters.

PHP
<?php
$words = ['cat', 'house', 'dog', 'elephant'];
$lengths = [];
foreach ($words as $word) {
    if ([1]($word) [2] 3) {
        $lengths[$word] = strlen($word);
    }
}
?>
Drag options to blanks, or click blank then click option'
Astrlen
B>
C<
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using count which works on arrays, not strings.
Using < instead of > causing wrong filtering.
5fill in blank
hard

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

PHP
<?php
$data = ['a' => 5, 'b' => 15, 'c' => 20];
$result = [];
foreach ($data as [1] => [2]) {
    if ([2] > 10) {
        $result[strtoupper([1])] = [2];
    }
}
?>
Drag options to blanks, or click blank then click option'
A$key
B$value
C$val
D$k
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up key and value variable names.
Using inconsistent variable names inside the loop.

Practice

(1/5)
1. What does the foreach loop do in PHP when used with an array?
easy
A. It only visits the first item in the array.
B. It visits each item in the array one by one.
C. It deletes all items in the array.
D. It reverses the order of the array.

Solution

  1. Step 1: Understand the purpose of foreach

    The foreach loop is designed to go through each element in an array, one at a time.
  2. Step 2: Compare options with foreach behavior

    Only It visits each item in the array one by one. correctly describes visiting each item. Others describe actions foreach does not perform.
  3. Final Answer:

    It visits each item in the array one by one. -> Option B
  4. Quick Check:

    foreach loops visit all items [OK]
Hint: Remember: foreach loops through all array items [OK]
Common Mistakes:
  • Thinking foreach only visits first item
  • Confusing foreach with array modification functions
  • Assuming foreach changes array order
2. Which of the following is the correct syntax to loop through an array $colors using foreach to get each value?
easy
A. foreach ($colors in $color) { echo $color; }
B. foreach $colors as $color { echo $color; }
C. foreach ($colors as $color) { echo $color; }
D. foreach ($color as $colors) { echo $color; }

Solution

  1. Step 1: Identify correct foreach syntax

    The correct syntax is: foreach (array as value) { ... } with parentheses and 'as' keyword.
  2. Step 2: Check each option

    foreach ($colors as $color) { echo $color; } matches correct syntax. Options A, B, and D have syntax errors or wrong order.
  3. Final Answer:

    foreach ($colors as $color) { echo $color; } -> Option C
  4. Quick Check:

    Correct foreach syntax uses 'foreach (array as value)' [OK]
Hint: Use parentheses and 'as' keyword in foreach [OK]
Common Mistakes:
  • Missing parentheses around array and variable
  • Using 'in' instead of 'as'
  • Swapping variable and array positions
3. What will be the output of this PHP code?
$fruits = ['apple', 'banana', 'cherry'];
foreach ($fruits as $fruit) {
    echo $fruit . ' ';
}
medium
A. apple banana cherry
B. apple, banana, cherry
C. Array
D. applebanana cherry

Solution

  1. Step 1: Understand the loop output

    The foreach loop prints each fruit followed by a space.
  2. Step 2: Combine printed values

    Output will be 'apple ' then 'banana ' then 'cherry ' concatenated.
  3. Final Answer:

    apple banana cherry -> Option A
  4. Quick Check:

    Each fruit printed with space [OK]
Hint: Look for spaces after each echo in loop [OK]
Common Mistakes:
  • Expecting commas instead of spaces
  • Thinking array prints as string
  • Missing spaces between words
4. Find the error in this PHP code snippet:
$numbers = [1, 2, 3];
foreach ($numbers $num) {
    echo $num;
}
medium
A. Missing 'as' keyword in foreach loop.
B. Array should be a string.
C. Variable $num is not defined.
D. Echo statement is incorrect.

Solution

  1. Step 1: Check foreach syntax

    foreach requires 'as' keyword between array and variable.
  2. Step 2: Identify missing keyword

    The code misses 'as' between $numbers and $num, causing syntax error.
  3. Final Answer:

    Missing 'as' keyword in foreach loop. -> Option A
  4. Quick Check:

    foreach needs 'as' keyword [OK]
Hint: Always include 'as' in foreach syntax [OK]
Common Mistakes:
  • Omitting 'as' keyword
  • Confusing variable and array order
  • Assuming variables need prior declaration
5. Given the array $items = ['a' => 1, 'b' => 0, 'c' => 3, 'd' => null];, which foreach loop will print only keys with truthy values?
hard
A. foreach ($items as $key => $value) { echo $key; }
B. foreach ($items as $key => $value) { if ($value === 0) echo $key; }
C. foreach ($items as $value) { if ($value) echo $value; }
D. foreach ($items as $key => $value) { if ($value) echo $key; }

Solution

  1. Step 1: Understand truthy values in PHP

    Values like 1 and 3 are truthy; 0 and null are falsy.
  2. Step 2: Analyze each loop's condition

    foreach ($items as $key => $value) { if ($value) echo $key; } prints keys only if value is truthy, matching requirement. Others print keys regardless or check wrong condition.
  3. Final Answer:

    foreach ($items as $key => $value) { if ($value) echo $key; } -> Option D
  4. Quick Check:

    Check value truthiness before echo key [OK]
Hint: Use if ($value) to filter truthy values [OK]
Common Mistakes:
  • Printing keys without condition
  • Checking for zero instead of truthy
  • Not using key => value syntax
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