PHP floor syntax
floor(int|float $num): float
In PHP, floor rounds toward negative infinity. The floor of 2.3 is 2, the floor of −2.3 is −3, and 2 stays 2.
floor
<?php
echo (floor(2.8)), PHP_EOL;
2
floor(int|float $num): float
| Input | An int or float. |
|---|---|
| Result | A float, even when the rounded number is a whole number. |
A test bench increases a reading by one tenth of a unit at a time. Watch where the rounded display changes as the sample passes from one integer toward the next.
<?php
class MathExample {
public function getfloorValue() {
echo("The floor value of 1.0 is " . floor(1.0) . "\n");
echo("The floor value of 1.1 is " . floor(1.1) . "\n");
echo("The floor value of 1.2 is " . floor(1.2) . "\n");
echo("The floor value of 1.3 is " . floor(1.3) . "\n");
echo("The floor value of 1.4 is " . floor(1.4) . "\n");
echo("The floor value of 1.5 is " . floor(1.5) . "\n");
echo("The floor value of 1.6 is " . floor(1.6) . "\n");
echo("The floor value of 1.7 is " . floor(1.7) . "\n");
echo("The floor value of 1.8 is " . floor(1.8) . "\n");
echo("The floor value of 1.9 is " . floor(1.9) . "\n");
echo("The floor value of 2.0 is " . floor(2.0) . "\n");
}
}
$obj = new MathExample();
$obj->getfloorValue();
The floor value of 1.0 is 1 The floor value of 1.1 is 1 The floor value of 1.2 is 1 The floor value of 1.3 is 1 The floor value of 1.4 is 1 The floor value of 1.5 is 1 The floor value of 1.6 is 1 The floor value of 1.7 is 1 The floor value of 1.8 is 1 The floor value of 1.9 is 1 The floor value of 2.0 is 2
A scheduling display groups positive measurements into whole-unit intervals. Compare readings just on either side of an integer to see which interval the display would select.
<?php
class MathExample {
public function getfloorValue() {
echo("The floor value of 1.5698 is " . floor(1.5698) . "\n");
echo("The floor value of 1.3093 is " . floor(1.3093) . "\n");
echo("The floor value of 1.0087 is " . floor(1.0087) . "\n");
echo("The floor value of 1.1038 is " . floor(1.1038) . "\n");
echo("The floor value of 1.9573 is " . floor(1.9573) . "\n");
echo("The floor value of 1.7462 is " . floor(1.7462) . "\n");
echo("The floor value of 1.6427 is " . floor(1.6427) . "\n");
echo("The floor value of 1.4128 is " . floor(1.4128) . "\n");
echo("The floor value of 1.8099 is " . floor(1.8099) . "\n");
echo("The floor value of 1.2743 is " . floor(1.2743) . "\n");
echo("The floor value of 2.0001 is " . floor(2.0001) . "\n");
}
}
$obj = new MathExample();
$obj->getfloorValue();
The floor value of 1.5698 is 1 The floor value of 1.3093 is 1 The floor value of 1.0087 is 1 The floor value of 1.1038 is 1 The floor value of 1.9573 is 1 The floor value of 1.7462 is 1 The floor value of 1.6427 is 1 The floor value of 1.4128 is 1 The floor value of 1.8099 is 1 The floor value of 1.2743 is 1 The floor value of 2.0001 is 2
A temperature logger records values just below zero or another integer boundary. Follow the rounding direction on the negative side before using the result as the label of a chart interval.
<?php
class MathExample {
public function getfloorValue() {
echo("The floor value of -1.5698 is " . floor(-1.5698) . "\n");
echo("The floor value of -1.3093 is " . floor(-1.3093) . "\n");
echo("The floor value of -1.0087 is " . floor(-1.0087) . "\n");
echo("The floor value of -1.1038 is " . floor(-1.1038) . "\n");
echo("The floor value of -1.9573 is " . floor(-1.9573) . "\n");
echo("The floor value of -1.7462 is " . floor(-1.7462) . "\n");
echo("The floor value of -1.6427 is " . floor(-1.6427) . "\n");
echo("The floor value of -1.4128 is " . floor(-1.4128) . "\n");
echo("The floor value of -1.8099 is " . floor(-1.8099) . "\n");
echo("The floor value of -1.2743 is " . floor(-1.2743) . "\n");
echo("The floor value of -2.0001 is " . floor(-2.0001) . "\n");
}
}
$obj = new MathExample();
$obj->getfloorValue();
The floor value of -1.5698 is -2 The floor value of -1.3093 is -2 The floor value of -1.0087 is -2 The floor value of -1.1038 is -2 The floor value of -1.9573 is -2 The floor value of -1.7462 is -2 The floor value of -1.6427 is -2 The floor value of -1.4128 is -2 The floor value of -1.8099 is -2 The floor value of -1.2743 is -2 The floor value of -2.0001 is -3
A sensor can use infinity to mark a reading outside its measuring range. Pass both signs through this operation so the display can handle the result or the reported error deliberately.
<?php
class MathExample {
public function getfloorValue() {
// Positive ∞ infinite value
echo("The floor value of INF is " . floor(INF) . "\n");
// Negative ∞ infinite value
echo("The floor value of -INF is " . floor(-INF) . "\n");
}
}
$obj = new MathExample();
$obj->getfloorValue();
The floor value of INF is INF The floor value of -INF is -INF
The costume room has 8.5 metres of ribbon left before a performance. Each bag needs one unbroken 1.2-metre strap. The volunteer needs the number of bags she can finish from this roll.
<?php
$metres = 8.5;
$perStrap = 1.2;
echo (int) floor($metres / $perStrap), " straps";
7 straps
Seven straps use 8.4 metres. The last tenth of a metre cannot make another strap.
Reference: PHP numeric operations