Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q5 of 15
PHP - Output and String Handling
What will be the output of this PHP code?
$price = 9.99;
echo sprintf("Price: $%.2f", $price);
APrice: 9.99$
BPrice: $9.9
CPrice: $9.9900
DPrice: $9.99
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the format string

    "Price: $%.2f" means print 'Price: $' followed by a float with 2 decimals.
  2. Step 2: Format the price 9.99

    9.99 formatted with 2 decimals remains 9.99.
  3. Final Answer:

    Price: $9.99 -> Option D
  4. Quick Check:

    %.2f formats float to 2 decimals [OK]
Quick Trick: Use %.2f to format floats with 2 decimals [OK]
Common Mistakes:
  • Placing $ inside format specifier
  • Using %d for floats
  • Forgetting to escape $ in double quotes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes