Bird
0
0

Why do escape sequences like \n and \t only work inside double-quoted strings in PHP and not single-quoted strings?

hard📝 Conceptual Q10 of 15
PHP - Output and String Handling
Why do escape sequences like \n and \t only work inside double-quoted strings in PHP and not single-quoted strings?
ABecause single quotes are deprecated in PHP
BBecause single-quoted strings treat backslash literally except for \' and \\
CBecause double quotes are faster to process
DBecause escape sequences are only valid in heredoc syntax
Step-by-Step Solution
Solution:
  1. Step 1: Understand PHP string parsing rules

    Single-quoted strings treat most backslashes literally except for escaping single quote and backslash itself.
  2. Step 2: Reason why escape sequences don't work in single quotes

    Escape sequences like \n and \t are only parsed inside double-quoted strings.
  3. Final Answer:

    Because single-quoted strings treat backslash literally except for \' and \\ -> Option B
  4. Quick Check:

    Escape sequences only parsed in double quotes [OK]
Quick Trick: Escape sequences only work in double-quoted strings [OK]
Common Mistakes:
  • Thinking single quotes parse escapes
  • Believing single quotes are deprecated
  • Confusing heredoc with single quotes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes