Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The correct way to assign a float value is using a number with a decimal point like 3.14.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name without the dollar sign.
Passing a string instead of the variable.
✗ Incorrect
Use the variable $number inside number_format to format the float.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing floats directly with ==.
Using a large tolerance like 1 which is too big.
✗ Incorrect
Because of floating point precision, we check if the difference is less than a small number like 0.0001 to consider them equal.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using integers instead of floats.
Mixing up the order of numbers.
✗ Incorrect
The array contains floats 1.5 and 2.5. Their sum is 4.0.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the float directly in round without assigning.
Forgetting to specify the number of decimals.
✗ Incorrect
Assign 3.6789 to $value, then round $value to 2 decimal places.