PHP ceil() Function

The ceil() function is a built-in mathematical function in PHP that rounds any float number up to the next highest integer. This function always returns a float number, as the range of float is larger than that of integer.

Syntax

ceil ( float $num ) : float

Parameters

Parameter Description
num The number to be rounded up

Return Value

The ceil() function returns the smallest integer value (as float) that is greater than or equal to the given parameter.

Examples

Example 1: Basic Usage

Here's a simple example demonstrating ceil() with negative numbers ?

<?php
    $arg = -3.95;
    $val = ceil($arg);
    echo "ceil(" . $arg . ") = " . $val;
?>
ceil(-3.95) = -3

Example 2: Multiple Test Cases

Let's test ceil() with various positive and negative numbers ?

<?php
    $numbers = [4.3, 9.999, -3.14, 0, 5.0, -0.5];
    
    foreach ($numbers as $num) {
        echo "ceil($num) = " . ceil($num) . "<br>";
    }
?>
ceil(4.3) = 5
ceil(9.999) = 10
ceil(-3.14) = -3
ceil(0) = 0
ceil(5) = 5
ceil(-0.5) = 0

Key Points

  • ceil() always rounds up towards positive infinity
  • For negative numbers, rounding up means getting closer to zero
  • Integer values remain unchanged but are returned as float
  • Available in PHP 4.x, 5.x, 7.x, and 8.x

Conclusion

The ceil() function is essential for mathematical operations requiring upward rounding. Remember that it always rounds towards positive infinity, making negative numbers less negative.

Updated on: 2026-03-15T08:55:27+05:30

415 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements