0
0
PHPprogramming~10 mins

Float type and precision in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a float variable with value 3.14.

PHP
<?php
$pi = [1];
echo $pi;
?>
Drag options to blanks, or click blank then click option'
A3.14
B"3.14"
C3,14
Dfloat(3.14)
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the number makes it a string, not a float.
Using a comma instead of a dot for decimal point.
2fill in blank
medium

Complete the code to print the float value with 2 decimal places.

PHP
<?php
$number = 3.14159;
echo number_format([1], 2);
?>
Drag options to blanks, or click blank then click option'
Anumber
B$number
C3.14159
D"3.14159"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name without the dollar sign.
Passing a string instead of the variable.
3fill in blank
hard

Fix the error in the code to correctly compare two float values.

PHP
<?php
$a = 0.1 + 0.2;
$b = 0.3;
if (abs($a - $b) < [1]) {
    echo "Equal";
} else {
    echo "Not equal";
}
?>
Drag options to blanks, or click blank then click option'
A0.0001
B0.1
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing floats directly with ==.
Using a large tolerance like 1 which is too big.
4fill in blank
hard

Fill both blanks to create an array of floats and sum them.

PHP
<?php
$values = array([1], [2]);
$sum = array_sum($values);
echo $sum;
?>
Drag options to blanks, or click blank then click option'
A1.5
B2
C2.5
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using integers instead of floats.
Mixing up the order of numbers.
5fill in blank
hard

Fill all three blanks to create a float, round it, and print the result.

PHP
<?php
$value = [1];
$rounded = round([2], [3]);
echo $rounded;
?>
Drag options to blanks, or click blank then click option'
A3.6789
B$value
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the float directly in round without assigning.
Forgetting to specify the number of decimals.