Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q4 of 15
PHP - Variables and Data Types
What will be the output of the following PHP code?
$a = 0.1 + 0.2;
echo number_format($a, 1);
A0.1
B0.3
C0.2
D0.4
Step-by-Step Solution
Solution:
  1. Step 1: Calculate the sum of 0.1 and 0.2 in PHP

    Due to float precision, 0.1 + 0.2 is approximately 0.30000000000000004.
  2. Step 2: Apply number_format with 1 decimal place

    number_format rounds the value to 1 decimal place, resulting in '0.3'.
  3. Final Answer:

    0.3 -> Option B
  4. Quick Check:

    Float sum rounded = D [OK]
Quick Trick: Use number_format to round floats for display [OK]
Common Mistakes:
  • Expecting exact 0.3 without rounding
  • Confusing output with raw float
  • Ignoring number_format effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes