Bird
0
0

You want to print the sentence:

hard📝 Application Q8 of 15
PHP - Output and String Handling
You want to print the sentence:
"The total cost is $50" using PHP string interpolation. Which code snippet correctly does this?
Aecho "The total cost is $50";
Becho 'The total cost is $50';
Cecho "The total cost is \$50";
Decho "The total cost is ${50}";
Step-by-Step Solution
Solution:
  1. Step 1: Understand escaping $ in double quotes

    To print a literal dollar sign, escape it with backslash: \$
  2. Step 2: Check each option for correct output

    echo "The total cost is $50"; interpolates $50 as an empty variable (outputs "50"). echo "The total cost is \$50";: echo "The total cost is \$50"; escapes dollar sign correctly. echo 'The total cost is $50'; uses single quotes (no interpolation, $ literal). echo "The total cost is ${50}"; uses invalid variable syntax.
  3. Final Answer:

    echo "The total cost is \$50"; -> Option C
  4. Quick Check:

    Escape $ to print literal dollar sign [OK]
Quick Trick: Escape $ with \$ to print dollar sign inside double quotes [OK]
Common Mistakes:
  • Not escaping $ causing variable parsing error
  • Using single quotes expecting interpolation
  • Incorrect variable syntax with braces

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes