Bird
0
0

You want to print a string that includes a variable and a newline character in PHP. Which of the following code snippets correctly achieves this?

hard📝 Application Q15 of 15
PHP - Variables and Data Types
You want to print a string that includes a variable and a newline character in PHP. Which of the following code snippets correctly achieves this?
Aecho "User: $user\";
Becho 'User: $user\n';
Cecho 'User: ' . $user . '\n';
Decho "User: $user\n";
Step-by-Step Solution
Solution:
  1. Step 1: Check variable replacement and newline support

    Double quotes allow variable replacement and interpret escape sequences like \n as newline.
  2. Step 2: Analyze each option

    echo 'User: $user\n'; uses single quotes, so no variable replacement or newline. echo 'User: ' . $user . '\n'; concatenates but \n inside single quotes is literal, so no newline. echo "User: $user\"; has a syntax error due to unescaped double quote.
  3. Final Answer:

    echo "User: $user\n"; -> Option D
  4. Quick Check:

    Double quotes = variable + newline support [OK]
Quick Trick: Use double quotes for variables and special characters like \n [OK]
Common Mistakes:
  • Using single quotes expecting newline or variable replacement
  • Incorrect escaping of quotes inside strings
  • Concatenating but forgetting newline is literal in single quotes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes