Bird
0
0

Which of the following is the correct way to interpolate a variable $name inside a string in PHP?

easy📝 Syntax Q12 of 15
PHP - Output and String Handling
Which of the following is the correct way to interpolate a variable $name inside a string in PHP?
Aecho 'Hello, $name!';
Becho Hello, $name!;
Cecho "Hello, '$name'!";
Decho "Hello, $name!";
Step-by-Step Solution
Solution:
  1. Step 1: Check string delimiters for interpolation

    PHP interpolates variables only inside double quotes, not single quotes.
  2. Step 2: Analyze each option

    echo "Hello, $name!"; uses double quotes and variable $name correctly inside the string.
  3. Final Answer:

    echo "Hello, $name!"; -> Option D
  4. Quick Check:

    Double quotes + $variable = interpolation [OK]
Quick Trick: Use double quotes for variables inside strings [OK]
Common Mistakes:
  • Using single quotes which do not interpolate
  • Omitting quotes around the string
  • Putting variable inside single quotes inside double quotes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes