foreach requires 'as' keyword between array and variable.
Step 2: Identify missing keyword
The code misses 'as' between $numbers and $num, causing syntax error.
Final Answer:
Missing 'as' keyword in foreach loop. -> Option A
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
Step 1: Understand truthy values in PHP
Values like 1 and 3 are truthy; 0 and null are falsy.
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.
Final Answer:
foreach ($items as $key => $value) { if ($value) echo $key; } -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.