Bird
0
0

Why does the following PHP code print the string literal instead of the variable value?

hard📝 Conceptual Q10 of 15
PHP - Output and String Handling
Why does the following PHP code print the string literal instead of the variable value?
<?php
$name = "John";
echo 'Hello, $name!';
?>
ABecause echo cannot print variables
BBecause single quotes do not parse variables inside strings
CBecause variable $name is not defined
DBecause of missing semicolon
Step-by-Step Solution
Solution:
  1. Step 1: Understand string quoting rules in PHP

    Single-quoted strings treat everything literally, so variables are not replaced by their values.
  2. Step 2: Analyze the code behavior

    The code prints exactly 'Hello, $name!' because $name is inside single quotes.
  3. Final Answer:

    Because single quotes do not parse variables inside strings -> Option B
  4. Quick Check:

    Single quotes print variables literally [OK]
Quick Trick: Single quotes do not replace variables inside strings [OK]
Common Mistakes:
  • Assuming variables are parsed in single quotes
  • Thinking variable is undefined
  • Ignoring string quoting rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes