Bird
Raised Fist0
PHPprogramming~20 mins

Float type and precision in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Float Precision Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP float addition?

Consider the following PHP code:

$a = 0.1 + 0.2;
echo $a === 0.3 ? 'Equal' : 'Not equal';

What will be printed?

PHP
$a = 0.1 + 0.2;
echo $a === 0.3 ? 'Equal' : 'Not equal';
ANot equal
BEqual
C0.3
DError
Attempts:
2 left
💡 Hint

Think about how floating point numbers are stored in computers.

❓ Predict Output
intermediate
2:00remaining
What is the output of this float formatting code?

What does this PHP code output?

$num = 1.23456789;
echo number_format($num, 4);
PHP
$num = 1.23456789;
echo number_format($num, 4);
A1.2346
B1.2345
C1.23456789
D1.234
Attempts:
2 left
💡 Hint

number_format rounds the number to the specified decimals.

🔧 Debug
advanced
2:30remaining
Why does this float comparison fail?

Examine this PHP code snippet:

$x = 0.15 + 0.15;
$y = 0.1 + 0.2;
if ($x == $y) {
    echo 'Equal';
} else {
    echo 'Not equal';
}

Why does it print 'Not equal'?

PHP
$x = 0.15 + 0.15;
$y = 0.1 + 0.2;
if ($x == $y) {
    echo 'Equal';
} else {
    echo 'Not equal';
}
ABecause both sums are exactly equal but the code has a syntax error
BBecause 0.1 + 0.2 is exactly 0.3 but 0.15 + 0.15 is not
CBecause PHP does not support float addition
DBecause 0.15 + 0.15 is exactly 0.3 but 0.1 + 0.2 is not
Attempts:
2 left
💡 Hint

Try printing the values of $x and $y with high precision.

📝 Syntax
advanced
1:30remaining
Which option causes a syntax error with float literals?

Which of the following PHP code snippets will cause a syntax error?

A$num = 0.5;
B$num = 1.2e3;
C$num = 1.2.3;
D$num = 2e-2;
Attempts:
2 left
💡 Hint

Look carefully at the float literal formats.

🚀 Application
expert
2:30remaining
How many items are in the array after float keys are used?

What is the number of elements in this PHP array?

$arr = [];
$arr[1.0] = 'a';
$arr[1.00] = 'b';
$arr[1.000] = 'c';
echo count($arr);
PHP
$arr = [];
$arr[1.0] = 'a';
$arr[1.00] = 'b';
$arr[1.000] = 'c';
echo count($arr);
AError
B1
C3
D0
Attempts:
2 left
💡 Hint

Think about how PHP treats float keys in arrays.

Practice

(1/5)
1. What is the main reason floats in PHP can sometimes give unexpected results when compared directly?
easy
A. Because PHP does not support decimal numbers
B. Because floats are stored as strings internally
C. Because floats have limited precision and tiny rounding errors can occur
D. Because floats always round down to the nearest integer

Solution

  1. Step 1: Understand float storage

    Floats are stored in a way that cannot represent all decimal numbers exactly, causing small rounding errors.
  2. Step 2: Impact on comparisons

    These tiny errors mean direct equality checks (==) can fail unexpectedly.
  3. Final Answer:

    Because floats have limited precision and tiny rounding errors can occur -> Option C
  4. Quick Check:

    Float precision limits cause rounding errors [OK]
Hint: Remember floats can't represent all decimals exactly [OK]
Common Mistakes:
  • Thinking floats are stored as exact decimals
  • Assuming PHP treats floats as strings
  • Believing floats always round down
2. Which of the following is the correct way to round a float to 2 decimal places in PHP?
easy
A. round(3.14159)
B. round(3.14159, '1')
C. round(3.14159, 0.02)
D. round(3.14159, 2)

Solution

  1. Step 1: Check round() function usage

    The round() function takes a float and an integer number of decimal places as arguments.
  2. Step 2: Validate each option

    round(3.14159, 2) uses round(3.14159, 2) which rounds to 2 decimals correctly. round(3.14159, '1') uses a string '1' which is invalid. round(3.14159) rounds to 0 decimals by default. round(3.14159, 0.02) uses 0.02 which is not a valid precision argument.
  3. Final Answer:

    round(3.14159, 2) -> Option D
  4. Quick Check:

    round(number, decimals) needs integer decimals [OK]
Hint: Use round(value, 2) to get two decimals [OK]
Common Mistakes:
  • Passing decimals as string instead of integer
  • Using non-integer precision values
  • Forgetting the second argument rounds to zero decimals
3. What will be the output of the following PHP code?
$a = 0.1 + 0.2;
if ($a == 0.3) {
echo 'Equal';
} else {
echo 'Not Equal';
}
medium
A. Equal
B. Not Equal
C. Syntax Error
D. 0.3

Solution

  1. Step 1: Calculate $a value

    Adding 0.1 + 0.2 in floats results in a value very close but not exactly 0.3 due to float precision limits.
  2. Step 2: Compare $a with 0.3

    The equality check ($a == 0.3) fails because of tiny rounding differences, so the else branch runs.
  3. Final Answer:

    Not Equal -> Option B
  4. Quick Check:

    0.1 + 0.2 != 0.3 exactly in floats [OK]
Hint: Avoid direct float equality checks; expect rounding errors [OK]
Common Mistakes:
  • Assuming 0.1 + 0.2 equals exactly 0.3
  • Expecting 'Equal' output
  • Confusing syntax error with logic error
4. Identify the error in this PHP code snippet related to float precision:
$value = 1.23456789;
$rounded = round($value, 10);
echo $rounded;
medium
A. round() cannot round to more decimal places than the float precision
B. Syntax error in round() function call
C. The echo statement is missing a semicolon
D. No error; output will be 1.23456789

Solution

  1. Step 1: Understand float precision limits

    PHP floats typically have about 14 decimal digits precision, so rounding beyond that has no effect.
  2. Step 2: Analyze round() behavior

    Rounding to 10 decimals is allowed syntactically but cannot increase precision beyond what float stores.
  3. Final Answer:

    round() cannot round to more decimal places than the float precision -> Option A
  4. Quick Check:

    Float precision limits rounding accuracy [OK]
Hint: Rounding beyond float precision has no effect [OK]
Common Mistakes:
  • Thinking round() call is a syntax error
  • Expecting echo missing semicolon error
  • Assuming rounding always changes output
5. You want to compare two float values in PHP for equality but avoid errors due to precision. Which approach is best?
hard
A. Use an epsilon value and check if the absolute difference is less than epsilon
B. Compare floats directly with == operator
C. Convert floats to strings and compare strings
D. Round both floats to zero decimals and compare

Solution

  1. Step 1: Understand float comparison issues

    Direct comparison can fail due to tiny rounding errors in float representation.
  2. Step 2: Use epsilon for safe comparison

    Comparing if absolute difference is less than a small epsilon (like 0.00001) accounts for precision errors safely.
  3. Step 3: Evaluate other options

    Direct == comparison is unreliable. String conversion can cause formatting issues. Rounding to zero decimals loses precision.
  4. Final Answer:

    Use an epsilon value and check if the absolute difference is less than epsilon -> Option A
  5. Quick Check:

    Use epsilon difference for float equality [OK]
Hint: Compare floats using small epsilon difference [OK]
Common Mistakes:
  • Using == directly on floats
  • Comparing float strings instead of numbers
  • Rounding too aggressively losing precision
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard