Bird
0
0

Which of the following is the correct way to include a variable $age inside a double-quoted string in PHP?

easy📝 Conceptual Q2 of 15
PHP - Output and String Handling
Which of the following is the correct way to include a variable $age inside a double-quoted string in PHP?
Aecho "I am $age years old.";
Becho 'I am $age years old.';
Cecho "I am {$age years old}.";
Decho "I am $age years old";
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct interpolation syntax

    Variables inside double quotes are replaced by their values directly, so "$age" works.
  2. Step 2: Check other options for errors

    Single quotes do not interpolate; curly braces must enclose only variable names; missing semicolon is syntax error.
  3. Final Answer:

    echo "I am $age years old."; -> Option A
  4. Quick Check:

    Variable inside double quotes = interpolated [OK]
Quick Trick: Use double quotes and $variable for interpolation [OK]
Common Mistakes:
  • Using single quotes expecting interpolation
  • Misplacing curly braces around text
  • Omitting semicolon at end

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes