The ceil(), floor(), round(), and abs() functions in PHP are used to handle rounding logic, decimal values, and absolute numbers in mathematical calculations.
ceil() rounds numbers upward.floor() rounds numbers downward.round() rounds to the nearest value.abs() removes the sign of a number.ceil(float $num)floor(float $num)round(float $num, int $precision = 0)abs(int|float $num)
// Rounds the number up to the next integer
<?php
$number = 3.14;
echo ceil($number);
?>
The output will be 4 because ceil() always rounds upward.
// Rounds the number down to the nearest integer
<?php
$number = 3.14;
echo floor($number);
?>
The output will be 3 because floor() removes the decimal part.
// Rounds the value to the nearest integer
<?php
$number = 3.65;
echo round($number);
?>
The output will be 4 because the decimal value is greater than 0.5.
// Converts a negative number to a positive value
<?php
$number = -5;
echo abs($number);
?>
The output will be 5 because abs() removes the negative sign.
ceil() when rounding up is mandatory.floor() to prevent exceeding limits.round() with precision for financial values.abs() when only magnitude matters.round() precision values.