Bird
0
0

You have variables $name = 'Anna', $age = 28, and $city = 'Paris'. How do you create an array with only $name and $city using compact?

hard📝 Application Q8 of 15
PHP - Array Functions
You have variables $name = 'Anna', $age = 28, and $city = 'Paris'. How do you create an array with only $name and $city using compact?
A$arr = compact('name', 'age');
B$arr = compact('name', 'city');
C$arr = compact(name, city);
D$arr = compact(['name', 'city']);
Step-by-Step Solution
Solution:
  1. Step 1: Select variables to include

    We want only $name and $city in the array.
  2. Step 2: Use compact with correct variable names

    Pass variable names as strings to compact for those variables.
  3. Final Answer:

    $arr = compact('name', 'city'); -> Option B
  4. Quick Check:

    compact selects variables by name strings [OK]
Quick Trick: Pass only needed variable names as strings to compact [OK]
Common Mistakes:
  • Passing unquoted names
  • Including unwanted variables
  • Passing array instead of strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes